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

Source: Internet
Author: User

It seems that you have noticed that, no matter how defined, it is likeC ++ linked listInObjectAre of the same type. In fact, this is also necessary. Otherwise, what is the type of the return value of a function such as the data in the returned node? However, human requirements are endless. The purpose of putting different object chains in a linked list is to make it easy to use. Now we must remember this principle. The subsequent discussions are based on this principle.

The principle of achieving this goal is actually very simple. You only need to change different types of objects to the same type. See the following structure definition:

 
 
  1. struct Mobject  
  2. {  
  3.        void *p;  
  4.        int ObjectType;  
  5. }; 

When an object is linked to a linked list, the pointer pointing to this object is assigned to p, and the object type is recorded. When this node is obtained, the object type indicated by p is determined based on the ObjectType value, so as to restore the pointer type and obtain the original object.

The generalized table mentioned later actually uses this method. Obviously, the objects supported by such Mobject are pre-determined. You will maintain the ObjectType list by yourself, and each time you add a type of support, you need to give an alternative value in the ObjectType list, and then give this type of case statement in the corresponding switch (ObjectType. It's annoying, right? Another method is provided below. In fact, it's still the principle. The difference is that the annoying work is handed over to the compiler.

Do you still remember the principles highlighted in the front? Why do we put different types of objects in a linked list? Obviously, we want to achieve this: for example, we store triangle, straight line, circle, and other graphical parameters in a linked list. We want to use Draw () for a node () method to reproduce the image. Get () is used to obtain the parameters of the image. Put () is used to modify the parameters of the image. It can be seen that these different objects actually have the same behavior, but the implementation method is different.

The polymorphism of C ++ can achieve our vision. For more information, see related C ++ books (I am reading C ++ programming ideas). See the following example:

 
 
  1. #ifndef Shape_H   
  2. #define Shape_H   
  3. class Shape     
  4. {   
  5. public:   
  6.        virtual void Input() = 0;   
  7.        virtual void Print() = 0;   
  8.        Shape(){};   
  9.        virtual ~Shape(){};   
  10. };   
  11. #endif   

Description: defines an abstract base class, which has two behaviors: Input () is the Input image parameter, and Print () is the Print Image parameter. It is easy to explain the problem.

 
 
  1. # Ifndef Point_H  
  2. # Define Point_H  
  3. ClassPoint 
  4. Public: 
  5. VoidPut () 
  6. Cout <"X coordinate :"; 
  7. Cin> x; 
  8. Cout <"Y coordinate :"; 
  9. Cin> y; 
  10. VoidGet () 
  11. Cout <endl <"X coordinate :"<X; 
  12. Cout <endl <"Y coordinate :"<Y; 
  13. Virtual~ Point (){}; 
  14. Private: 
  15. IntX; 
  16. IntY; 
  17. }; 
  18. # Endif  

Description: class definition and implementation of a vertex.

 
 
  1. # Ifndef Circle_H  
  2. # Define Circle_H  
  3. # Include "Shape. h"  
  4. # Include "Point. h"  
  5. ClassCircle:PublicShape 
  6. Public: 
  7. VoidInput () 
  8. Cout <endl <"Input circle Parameters"; 
  9. Cout <endl <"Enter the coordinates of the center point :"<Endl; 
  10. Center. Put (); 
  11. Cout <endl <"Input radius :"; 
  12. Cin> radius; 
  13. VoidPrint () 
  14. Cout <endl <"The circle parameter is"; 
  15. Cout <endl <"Coordinates of the center point :"<Endl; 
  16. Center. Get (); 
  17. Cout <endl <"Radius :"<Radius; 
  18. Virtual~ Circle (){}; 
  19. Private: 
  20. IntRadius; 
  21. Point center; 
  22. }; 
  23. # Endif  

Description: class definition and implementation of a circle. Inherits the behavior of the Shape class.

 
 
  1. # Ifndef Line_H  
  2. # Define Line_H  
  3. # Include "Shape. h"  
  4. # Include "Point. h"  
  5. ClassLine:PublicShape 
  6. Public: 
  7. VoidInput () 
  8. Cout <endl <"Parameters of the input line"; 
  9. Cout <endl <"Coordinate of input endpoint 1 :"<Endl; 
  10. Point1.Put (); 
  11. Cout <endl <"Coordinate of input endpoint 2 :"<Endl; 
  12. Point2.Put (); 
  13. VoidPrint () 
  14. Cout <endl <"The straight line parameter is"; 
  15. Cout <endl <"Coordinate of endpoint 1 :"; 
  16. Point1.Get (); 
  17. Cout <endl <"Coordinate of endpoint 2 :"; 
  18. Point2.Get (); 
  19. Virtual~ Line (){}; 
  20. Private: 
  21. Point point1; 
  22. Point point2; 
  23. }; 
  24. # Endif 

Description: Linear class definition and implementation. Inherits the Shape action.

 
 
  1. #ifndef ListTest_H   
  2. #define ListTest_H   
  3. #include    
  4. #include "List.h"   
  5. #include "Circle.h"   
  6. #include "Line.h"   
  7. void ListTest_MObject()   
  8. {   
  9.        List  a;    
  10.        Shape *p1 = new Circle;   
  11.        Shape *p2 = new Line;   
  12.        p1->Input();   
  13.        p2->Input();   
  14.        a.Insert(p1);   
  15.        a.Insert(p2);   
  16.        Shape *p = *a.Next();   
  17.        p->Print();   
  18.        delete p;   
  19.        a.Put(NULL);   
  20.        p = *a.Next();   
  21.        p->Print();   
  22.        delete p;   
  23.        a.Put(NULL);   
  24. }   
  25. #endif   

Note: This is a test function. You can add # include "ListTest. h" to the header of the cpp file containing main (), and then call ListTest_Mobject (). This is a simple example. It can be seen that deleting a linked list node requires two steps: delete the object indicated by the pointer in the data field of the linked list node before deleting the linked list node. Also, pay attention to this issue when you analyze such a linked list. Otherwise, your program will run a little less memory at a time (this may not be the case, it is said that the operating system can reclaim the dynamic memory when the program is terminated, but the subsequent conclusion is correct ), if it is a function that is frequently called, your system will be paralyzed after running for a period of time. Therefore, it is best to use such a linked list to derive a new linked list class to implement corresponding operations. For example:

 
 
  1. class ShapeList : public List    
  2. {   
  3. public:   
  4.        BOOL SL_Remove()   
  5.        {   
  6.               Shape *p = *Get();   
  7.               delete p;   
  8.               return Remove();   
  9.        }  
  10. };  

Gossip: I wonder if you are not familiar with this statement Shape * p = * a. Next (); p-> Print (); I don't know much about it, but I still think it's a bit cool. Then try this statement * a. Next ()-> Print (); can it be compiled.

  1. Introduction to object-oriented programming in C ++
  2. Practical application skills of C ++ linked list operations
  3. Interpret and analyze the C ++ linked list
  4. Analysis of the C ++ Object Model
  5. Programmers must read the summary of c ++ pen questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.