 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Aug 29, 2006 9:11 am Post subject: child list. . |
|
|
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 (cmk128 (AT) hotmail (DOT) com) |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Sep 01, 2006 6:53 am Post subject: Re: child list. . |
|
|
mlimber 寫道:
| Quote: | cmk128 (AT) hotmail (DOT) com wrote:
What i am going to do is design a graphic framework. Suppose there is a
GraphicEngine class, it used to response for all the drawing to the
screen. Also suppose there is a class called Dialog. When user "new
Dialog()", the GraphicEngine should know it, and able to call
Dialog::GraphicEngine. That's why the GraphicEngine has to kow how many
Dialog object is existed and hold its reference (because to call its
onPaint method).
It doesn't sound to me like inheritance is the best representation of
this relationship (the general rule is to use the weakest relationship
that you can, cf. FAQ 24.3; inheritance is a strong relationship, while
relationships such as composition and oberver/callback are weaker). You
might consider making all drawable classes register with the
GraphicEngine in their constructors. To do this, either pass an
instance of GraphicEngine in as a constructor parameter or, if there's
only ever going to be one instance, make it a global singleton.
However, we're really getting away from the C++ language proper (the
topic of this group) and more into general software design. You should
ask questions on OO software design in another newsgroup such as
comp.object or similar. Of course, if you need help implementing the
design in C++, this would be the right group to ask in.
Cheers! --M
|
Thank you for your answer
Your suggestion is same as my original idea, register the class
itself to GraphicEngine class in the constructor. This is a rule, but
people may not follow, may be they just forget. So this is not good.
Let's have a look java, every class extends "Class component" is a
visual class, the sub-class doesn't need to change anything in the
construct. This is good design.
thanks
from Peter (cmk128 (AT) hotmail (DOT) com) |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|