parent class's child object list
Hi All
Is there any alternative way to let parent class hold a vector of
its child class's objects?
I meant when i create an object from the child class, in the parent
class, i am able to reference that object.
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class mother{
private:
protected:
vector <void *>list;
mother(){
}
public:
static mother * getInstance(){
static mother m;
return &m;
}
vector <void *> * getList(){
return &list;
}
};
class child : mother{
public:
child(){
cout<<"child()"<<endl;
mother::getInstance()->getList()->push_back((void *)this);
cout<<"list->size()="<<(int)mother::getInstance()->getList()->size()<<endl;
}
};
void dumpVector(vector <void *> *v){
for (int x=0;x<v->size();x++){
vector <void *>vv=*v;
int yy=(int)vv[x];
cout<<(*v)[x]<<endl;
}
}
int main(){
dumpVector(mother::getInstance()->getList());
child *c=new child();
dumpVector(mother::getInstance()->getList());
return 0;
}
thanks
from Peter (cmk128@hotmail.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]