We'll talk about inserting different types of objects in an STL list (Lists)

Source: Internet
Author: User
Tags setcell

See an article on your Web site: "Insert different types of objects in an STL list (Lists)." I feel that the answer has not pointed out the nature of the problem, so I put forward my point of view, I beg to correct. I think that the cause of the error is the pointer transformation process, the program did not point out the original prototype of the pointer, or, because did not find the correct subclass function address to invoke the error, my original code is as follows: With Dev-cpp g++ compiled.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
/**
* Parent class: Synobject
*/
Class Synobject {
Public:
Synobject ();
String GetClass ();
String ClassName;
};
Synobject::synobject ()
{
ClassName = "Synobject";
}
String Synobject::getclass ()
{
return className;
}
/**
* Sub-class 1:synpin
*/
Class Synpin:public Synobject {
string pin;
Public:
Synpin ();
void Setpin (string Pin);
String Getpin ();
Private
};
Synpin::synpin ()
{
ClassName = "Synpin";
}
void Synpin::setpin (String Pin)
{
pin = pin;
}
String Synpin::getpin ()
{
return pin;
}
/**
* Sub-class 2:syncell
*/
Class Syncell:public Synobject {
string cell;
Public:
Syncell ();
void Setcell (string Cell);
String Getcell ();
Private
};
Syncell::syncell ()
{
ClassName = "Syncell";
}
void Syncell::setcell (String Cell)
{
cell = cell;
}
String Syncell::getcell ()
{
return cell;
}
/**
* The system runs the main program
*/
int main ()
{
file://Build Objects
Synobject * PMYOBJECT;
Pmyobject = new Synobject;
Synpin * PMYPIN;
Pmypin = new Synpin;
Pmypin->setpin ("Mypin");
Syncell * Pmycell;
Pmycell = new Syncell;
Pmycell->setcell ("MyCell");
Insert Object
Vector<synobject *> Myvector;
Myvector.empty ();
Myvector.push_back (Pmyobject);
Myvector.push_back (Pmypin);
Myvector.push_back (Pmycell);
Calling Object
Vector<synobject *>::iterator Thisvector=myvector.begin ();
cout<< "program begin:" <<endl;
while (Thisvector!= myvector.end ())
{
cout << (**thisvector). GetClass () << Endl;
    
if ((**thisvector). GetClass (). Compare ("Syncell") = = 0)
{
cout << ((syncell**) thisvector)). Getcell () << Endl;
}
    
if ((**thisvector). GetClass (). Compare ("synpin") = = 0)
{
cout << (* * (synpin**) thisvector). Getpin () << Endl;
}
thisvector++;
}
}
End of program

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.