In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.