473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::getInst ance()->getList()->push_back((voi d *)this);
cout<<"list->size()="<<(int )mother::getIns tance()->getList()->size()<<endl ;
}
};

void dumpVector(vect or <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(moth er::getInstance ()->getList());
child *c=new child();
dumpVector(moth er::getInstance ()->getList());
return 0;
}

thanks
from Peter (cm****@hotmail .com)

Aug 29 '06 #1
5 2001
cm****@hotmail. com wrote:
Hi
In the following code, when you create a object of class child,
parent class (class mother) will hold a reference.
No. Your "child" is also a "mother," so it inherits a "list" member.
But when you call mother::getInst ance(), you are using a distinct
instance of mother, not the one that is part of the child object. I
don't think that's what you wanted. Perhaps you intended the "list"
member to be static instead?
But i want every
class derived from mother class has this ability without changing its
constructor. how to?
You'll have to clarify what your real intention is before we can say
for sure. I would also note that void pointers should generally be
avoided in C++ since they throw away type information that you will
usually have to add back in at a later point and at your own peril.
#include <string>
#include <vector>
#include <iostream>

using namespace std;

class mother{
private:
protected:
vector <void *>list;
Compare
http://www.parashift.com/c++-faq-lit....html#faq-19.8
..
mother(){
}
public:
static mother * getInstance(){
static mother m;
return &m;
}

vector <void ** getList(){
return &list;
}
};

class child : mother{
public:
child(){
cout<<"child()" <<endl;
mother::getInst ance()->getList()->push_back((voi d *)this);
cout<<"list->size()="<<(int )mother::getIns tance()->getList()->size()<<endl ;
}
};

void dumpVector(vect or <void **v){
for (int x=0;x<v->size();x++){
vector <void *>vv=*v;
int yy=(int)vv[x];
These lines are never used. The former makes a copy of the vector,
which could be expensive.
cout<<(*v)[x]<<endl;
This will print an address. Is that what you want?
}
}

int main(){
dumpVector(moth er::getInstance ()->getList());
child *c=new child();
dumpVector(moth er::getInstance ()->getList());
return 0;
}
You didn't delete c. Of course, the OS will probably reclaim it since
you're exiting, but it's still good practice to clean up after yourself
(or better, use a smart pointer, e.g., std::auto_ptr<c hild>).

Cheers! --M

Aug 29 '06 #2

mlimber 寫道:
cm****@hotmail. com wrote:
Hi
In the following code, when you create a object of class child,
parent class (class mother) will hold a reference.

No. Your "child" is also a "mother," so it inherits a "list" member.
But when you call mother::getInst ance(), you are using a distinct
instance of mother, not the one that is part of the child object. I
don't think that's what you wanted. Perhaps you intended the "list"
member to be static instead?
But i want every
class derived from mother class has this ability without changing its
constructor. how to?

You'll have to clarify what your real intention is before we can say
for sure. I would also note that void pointers should generally be
avoided in C++ since they throw away type information that you will
usually have to add back in at a later point and at your own peril.
#include <string>
#include <vector>
#include <iostream>

using namespace std;

class mother{
private:
protected:
vector <void *>list;

Compare
http://www.parashift.com/c++-faq-lit....html#faq-19.8
.
mother(){
}
public:
static mother * getInstance(){
static mother m;
return &m;
}

vector <void ** getList(){
return &list;
}
};

class child : mother{
public:
child(){
cout<<"child()" <<endl;
mother::getInst ance()->getList()->push_back((voi d *)this);
cout<<"list->size()="<<(int )mother::getIns tance()->getList()->size()<<endl ;
}
};

void dumpVector(vect or <void **v){
for (int x=0;x<v->size();x++){
vector <void *>vv=*v;
int yy=(int)vv[x];

These lines are never used. The former makes a copy of the vector,
which could be expensive.
cout<<(*v)[x]<<endl;

This will print an address. Is that what you want?
}
}

int main(){
dumpVector(moth er::getInstance ()->getList());
child *c=new child();
dumpVector(moth er::getInstance ()->getList());
return 0;
}

You didn't delete c. Of course, the OS will probably reclaim it since
you're exiting, but it's still good practice to clean up after yourself
(or better, use a smart pointer, e.g., std::auto_ptr<c hild>).

Cheers! --M
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::Graphic Engine. That's why the GraphicEngine has to kow how many
Dialog object is existed and hold its reference (because to call its
onPaint method).
thanks
from Peter (cm****@hotmail .com)

Aug 31 '06 #3
cm****@hotmail. 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::Graphic Engine. 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

Aug 31 '06 #4

mlimber 寫道:
cm****@hotmail. 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::Graphic Engine. 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 (cm****@hotmail .com)

Sep 1 '06 #5
cm****@hotmail. com wrote:
mlimber 寫道:
cm****@hotmail. com wrote:
What i am going to do is design a graphic framework. Suppose there isa
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::Graphic Engine. 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.
So create an abstract base class that all drawable objects inherit from
and have its constructor do the registration with the same technique I
described in my previous post. You should not inherit from the object
that you're registering with (in your case GraphicEngine), however.

Cheers! --M

Sep 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3797
by: Bostonasian | last post by:
I am trying to append options to dropdown in parent window from option items in child window. In parent window, I have following code: <script language="javascript"> function AddItem2DropDown(item){ exists = false; for(d=0;d<drpDwn.length;d++){ if(drpDwn.options.value == item.value)
3
19756
by: maricel | last post by:
Is there a way to list (using db2 command or catalogs) to list hierarchy of table parent & child relationship: 1) A list that shows which table should be deleted first,second,third... 2) A list that shows which table should loaded first, second,third... Your help would be highly appreciated. maricel.
3
5606
by: James Spibey | last post by:
Hi, I have an MDI application which has aboout 10 child windows. The way the app needs to work is that only one window should be visible at a time and it should be maximized within the parent window. I have set all my child windows to be WindowState.Maximized but after showing 2 or 3 windows, the windows all drop back to Normal state. I...
4
4558
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as adding to the child collection, eg: parent.Children.Add(child); child.Parent = parent;
10
3999
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms,...
4
10973
by: Steve Barnett | last post by:
I've created a simple MDI application and have designated the Window menu to keep track of the mdi children. When I first load an mdi child, it's caption consists of "File: no file loaded" and this is what appears on the Window menu. Now, after the mdi child loads, I call a method to load a specifically named file. At this point, the child...
3
11310
by: reachsamdurai | last post by:
Is it possible to determine the list of child/parent tables for a particular table from any system catalog tables? Using the syscat.tables I'm able to retrieve the no of dependent parent/child tables but unable to determine the list of dependent table name(s) Example : For a simple department-employee relationship, the following query...
5
2084
by: cmk128 | last post by:
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>
0
1598
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi just wondering if anyone knows if there is a way to tell if a child window is still open in the code behind in the parent window (web application vs.net 2005)? I have a web app and am using the following to add an attribute to the body tag, body.Attributes.Add("onunload", "opener.location.href=opener.location.href;");. This causes the...
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.