Get the App
SLTechnology News&Howtos  ›  Development  › 

How to link different types of objects in the C++ linked list

Shulou Source: shulou.com Published: 2022-06-02 09:29:14 09月23日 Update

This article mainly introduces "how to link different types of objects in the C++ linked list". In daily operation, I believe many people have doubts about how to link different types of objects in the C++ linked list. I have consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to link different types of objects in the C++ linked list". Next, please follow the editor to study!

The principle of achieving this goal is actually very simple, as long as different types of objects are changed to the same type. Look at the following structure definition:

Struct Mobject {void * p; int ObjectType;}

When an object is linked to a linked list, a pointer to the object is assigned to p, and the object type is recorded. When this node is obtained, the object type that p refers to is determined according to the value of ObjectType, thus the pointer type is restored, and the original object is obtained.

This is actually the approach used in the generalized tables that are discussed later. Obviously, the object supported by such Mobject is predetermined, you will maintain the ObjectType list yourself, and each time you add a type of support, you need to give its alternative value in the ObjectType list, and then give this type of case statement in the corresponding switch (ObjectType). It's annoying, isn't it? here's another way, but it's the same principle, but the difference is, leave the annoying job to the compiler.

Remember the principle emphasized earlier, why do we put different types of objects in a linked list? Obviously, we want to achieve such an effect: for example, we store the parameters of triangles, lines, circles and other graphics in a linked list, and we want to use the Draw () method on a node to redraw the graph; use Get () to get the parameters of the graph; use Put () to modify the parameters of the graph. As you can see, these different objects actually have the same behavior, but in different ways.

The polymorphism of C++ can just realize our idea. In this regard, please refer to the relevant C++ books (I read "C++ programming ideas"). Take a look at the following example:

# ifndef Shape_H # define Shape_H class Shape {public: virtual void Input () = 0; virtual void Print () = 0; Shape () {}; virtual ~ Shape () {};}; # endif

[description] defines an abstract base class with two behaviors, Input () as the input graphic parameter and Print () as the print graphic parameter. To save trouble, it is just a simple illustration.

# ifndef Point_H # define Point_H class Point {public: void Put () {cout > x; cout > y;} void Get () {cout

Tags: Objects types different parameters coordinates graphics methods inputs nodes lines endpoints behaviors learning pointers problems annoyance two examples memory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Huawei Shulou Technology Microsoft