473,698 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parent's child 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::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)

Sep 18 '06 #1
5 2089
cm****@hotmail. com wrote:
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::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++){
What is the purpose of the following two lines?
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;

}
What is the problem that you are trying to solve?

Anyway, here is an idea without void* and without casting:

#include <vector>
#include <string>
#include <iostream>
class mother {

typedef mother* pointer;
typedef std::vector< pointer instance_list;

static
instance_list &
get_instances ( void ) {
static instance_list instances;
return ( instances );
}

public:

mother ( void ) {
get_instances() .push_back( this );
}

virtual
std::string get_name ( void ) const {
return ( "mother" );
}

static
void dump ( std::ostream& o_str ) {
for ( instance_list:: const_iterator iter = get_instances() .begin();
iter != get_instances() .end(); ++iter ) {
o_str << (*iter)->get_name() << '\n';
}
}

virtual
~mother ( void ) {}

};
class child_a : public mother {
public:

child_a ( void ) : mother() {}

std::string get_name ( void ) const {
return ( "child_a" );
}

};

class child_b : public mother {
public:

child_b ( void ) : mother() {}

std::string get_name ( void ) const {
return ( "child_b" );
}

};
int main ( void ) {
mother a;
child_a b;
mother c;
child_a d;
child_b e;
mother::dump( std::cout );
}

Best

Kai-Uwe Bux
Sep 18 '06 #2
Kai-Uwe Bux wrote:
cm****@hotmail. com wrote:
>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::getIns tance()->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++){

What is the purpose of the following two lines?
> 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;

}

What is the problem that you are trying to solve?

Anyway, here is an idea without void* and without casting:

#include <vector>
#include <string>
#include <iostream>
class mother {

typedef mother* pointer;
typedef std::vector< pointer instance_list;

static
instance_list &
get_instances ( void ) {
static instance_list instances;
return ( instances );
}

public:

mother ( void ) {
get_instances() .push_back( this );
}

virtual
std::string get_name ( void ) const {
return ( "mother" );
}

static
void dump ( std::ostream& o_str ) {
for ( instance_list:: const_iterator iter = get_instances() .begin();
iter != get_instances() .end(); ++iter ) {
o_str << (*iter)->get_name() << '\n';
}
}

virtual
~mother ( void ) {}

};
[test code snipped]

Ok, I reconsidered. The above has some very visible shortcomings. In
particular, it only adds to the list but it never removes pointers from the
list when the pointees are destructed -- very bad. Also, I forgot about
copy constructors, ...

Here is another approach packaging the solution into a policy template from
which you derive:

#include <set>

template < typename T >
struct tracked {

typedef T * pointer;
typedef T & reference;

private:

typedef tracked * base_pointer;
typedef std::set< base_pointer set_type;

public:

typedef typename set_type::size_ type size_type;

struct obj_iterator : public set_type::const _iterator {

obj_iterator ( typename set_type::const _iterator it )
: set_type::const _iterator( it )
{}

reference operator* ( void ) const {
return ( * up_cast( set_type::const _iterator::oper ator*() ) );
}

pointer operator-( void ) const {
return ( up_cast( set_type::const _iterator::oper ator*() ) );
}

};

static
bool has_objects ( void ) {
return( ! the_set().empty () );
}

static
bool is_valid ( pointer * ptr ) {
return ( the_set().find( ptr ) != the_set().end() );
}

static
size_type num_objects ( void ) {
return ( the_set().size( ) );
}

static
obj_iterator obj_begin ( void ) {
return ( the_set().begin () );
}

static
obj_iterator obj_end ( void ) {
return ( the_set().end() );
}

protected:

tracked ( void ) {
the_set().inser t( this );
}

tracked ( tracked const & ) {
the_set().inser t( this );
}

~tracked ( void ) {
the_set().erase ( this );
}

static
void create_set ( void ) {
the_set();
}

private:

static
pointer up_cast ( base_pointer bp ) {
return ( static_cast< pointer >( bp ) );
}

static
set_type & the_set ( void ) {
static set_type dummy;
return ( dummy );
}

}; // tracked
#include <iostream>

struct mother : public tracked< mother {

virtual
std::string get_name ( void ) const {
return ( "mother" );
}

virtual
~mother ( void ) {
std::cout << "destroying " << get_name() << ".\n";
}

};

class child_a : public mother {
public:

child_a ( void ) : mother() {}

std::string get_name ( void ) const {
return ( "child_a" );
}

virtual
~child_a ( void ) {
std::cout << "destroying " << get_name() << ", ";
}

};

class child_b : public mother {
public:

child_b ( void ) : mother() {}

std::string get_name ( void ) const {
return ( "child_b" );
}

virtual
~child_b ( void ) {
std::cout << "destroying " << get_name() << ", ";
}

};

int main ( void ) {
new mother;
new child_a;
new mother;
new child_b;
new child_a;
for ( mother::obj_ite rator iter = mother::obj_beg in();
iter != mother::obj_end (); ++iter ) {
std::cout << iter->get_name() << '\n';
}
while ( mother::has_obj ects() ) {
delete ( mother::obj_beg in().operator->() );
}
}
Best

Kai-Uwe Bux
Sep 21 '06 #3

Kai-Uwe Bux 寫道:
Kai-Uwe Bux wrote:
cm****@hotmail. com wrote:
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::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++){
What is the purpose of the following two lines?
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;

}
What is the problem that you are trying to solve?

Anyway, here is an idea without void* and without casting:

#include <vector>
#include <string>
#include <iostream>
class mother {

typedef mother* pointer;
typedef std::vector< pointer instance_list;

static
instance_list &
get_instances ( void ) {
static instance_list instances;
return ( instances );
}

public:

mother ( void ) {
get_instances() .push_back( this );
}

virtual
std::string get_name ( void ) const {
return ( "mother" );
}

static
void dump ( std::ostream& o_str ) {
for ( instance_list:: const_iterator iter = get_instances() .begin();
iter != get_instances() .end(); ++iter ) {
o_str << (*iter)->get_name() << '\n';
}
}

virtual
~mother ( void ) {}

};
[test code snipped]

Ok, I reconsidered. The above has some very visible shortcomings. In
particular, it only adds to the list but it never removes pointers from the
list when the pointees are destructed -- very bad. Also, I forgot about
copy constructors, ...

Here is another approach packaging the solution into a policy template from
which you derive:

#include <set>

template < typename T >
struct tracked {

typedef T * pointer;
typedef T & reference;

private:

typedef tracked * base_pointer;
typedef std::set< base_pointer set_type;

public:

typedef typename set_type::size_ type size_type;

struct obj_iterator : public set_type::const _iterator {

obj_iterator ( typename set_type::const _iterator it )
: set_type::const _iterator( it )
{}

reference operator* ( void ) const {
return ( * up_cast( set_type::const _iterator::oper ator*() ) );
}

pointer operator-( void ) const {
return ( up_cast( set_type::const _iterator::oper ator*() ) );
}

};

static
bool has_objects ( void ) {
return( ! the_set().empty () );
}

static
bool is_valid ( pointer * ptr ) {
return ( the_set().find( ptr ) != the_set().end() );
}

static
size_type num_objects ( void ) {
return ( the_set().size( ) );
}

static
obj_iterator obj_begin ( void ) {
return ( the_set().begin () );
}

static
obj_iterator obj_end ( void ) {
return ( the_set().end() );
}

protected:

tracked ( void ) {
the_set().inser t( this );
}

tracked ( tracked const & ) {
the_set().inser t( this );
}

~tracked ( void ) {
the_set().erase ( this );
}

static
void create_set ( void ) {
the_set();
}

private:

static
pointer up_cast ( base_pointer bp ) {
return ( static_cast< pointer >( bp ) );
}

static
set_type & the_set ( void ) {
static set_type dummy;
return ( dummy );
}

}; // tracked
#include <iostream>

struct mother : public tracked< mother {

virtual
std::string get_name ( void ) const {
return ( "mother" );
}

virtual
~mother ( void ) {
std::cout << "destroying " << get_name() << ".\n";
}

};

class child_a : public mother {
public:

child_a ( void ) : mother() {}

std::string get_name ( void ) const {
return ( "child_a" );
}

virtual
~child_a ( void ) {
std::cout << "destroying " << get_name() << ", ";
}

};

class child_b : public mother {
public:

child_b ( void ) : mother() {}

std::string get_name ( void ) const {
return ( "child_b" );
}

virtual
~child_b ( void ) {
std::cout << "destroying " << get_name() << ", ";
}

};

int main ( void ) {
new mother;
new child_a;
new mother;
new child_b;
new child_a;
for ( mother::obj_ite rator iter = mother::obj_beg in();
iter != mother::obj_end (); ++iter ) {
std::cout << iter->get_name() << '\n';
}
while ( mother::has_obj ects() ) {
delete ( mother::obj_beg in().operator->() );
}
}
Best

Kai-Uwe Bux
Hi i found a easy solution
in mother's constructor, just add the *this* pointer to the list

Sep 21 '06 #4
pa************* *************@h otmail.com wrote:
>
Kai-Uwe Bux ???
>Kai-Uwe Bux wrote:
cm****@hotmail. com wrote:

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.
[tons of code snipped]
>
Hi i found a easy solution
in mother's constructor, just add the *this* pointer to the list
Well, that is more or less what I had in my first reply along with some test
code to demonstrate that it works, and it still is at the core of the
convoluted thing I submitted finally. But in fact, that alone does not
quite cut it: One problem is that you also need the destructor to remove
the pointer from the list, which makes std::set a better data structure for
this problem. Another problem is that you really do not want to expose the
list anyway since you should guarantee that it is *only* changed through
constructors/destructors of mother. These considerations led to the
somewhat longer approach. Wrapping the whole mess up into the tracked<>
template is just some syntactic sugar to turn the solution into something
generic I could add to my library. (It also makes maintenance of mother
easier: if you add a constructor, there is no danger to forget the update
of the list.)
Best

Kai-Uwe Bux
Sep 21 '06 #5

Kai-Uwe Bux wrote:
Well, that is more or less what I had in my first reply along with some test
code to demonstrate that it works, and it still is at the core of the
convoluted thing I submitted finally. But in fact, that alone does not
quite cut it: One problem is that you also need the destructor to remove
the pointer from the list, which makes std::set a better data structure for
this problem. Another problem is that you really do not want to expose the
list anyway since you should guarantee that it is *only* changed through
constructors/destructors of mother. These considerations led to the
somewhat longer approach. Wrapping the whole mess up into the tracked<>
template is just some syntactic sugar to turn the solution into something
generic I could add to my library. (It also makes maintenance of mother
easier: if you add a constructor, there is no danger to forget the update
of the list.)
And then there's the multi-threading. There are a number of different
ways it should behave depending on what the instance list/set is used
for.

We have loads of slightly different implementations of this sort of
idea throughout our framework. I think it should be possible to hide it
all in library code by using an extra 'policy' template that implements
that part of it, but I've not spent any time thinking through how that
should work yet.
K

Sep 21 '06 #6

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

Similar topics

2
3813
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
19812
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.
4
4564
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
4016
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, remove the controls on each of them, and then close the form, but this code should execute only...
3
11358
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 gives the count of parent/child tables but not the table names(s). db2 => select tabname,...
11
2852
by: manstey | last post by:
Hi, I am having trouble designing my classes. I have two classes. The first one wraps around an old-style class called oref Class CacheClass(object): def __init__(self, obj):
4
2687
by: hellboss | last post by:
Hi everyone ! Im using sq05 I have a set of table which goes to 5 levels of Transation , ex: Parent 1 Parent 2(Child of Parent1) Parent 3(Child of Parent2) Parent 4(Child of Parent3) Child (Child of Parent4)
0
1607
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 parent page to reload anytime the child page has a post back event or closes. Also I am getting an...
0
8668
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 usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8855
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
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
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.