C
cmk128
Hi
In the following code, when you create a object of class child,
parent class (class mother) will hold a reference. But i want every
class derived from mother class has this ability without changing its
constructor. how to?
#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 ([email protected])
In the following code, when you create a object of class child,
parent class (class mother) will hold a reference. But i want every
class derived from mother class has this ability without changing its
constructor. how to?
#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 ([email protected])