accessing the member of parent class

F

free2cric

Hi I have a simple problem

class aaaa
{
public:
int a;
public:
int getParentVar()
}

class bbb
{
public:
int b;
aaaa google;
}

int aaaa::getParentVar()
{
/* access class bbb's variable that is int b
}

so please tell me how to get int b of class bbb in the method
getParentVar of class aaa.

Thanks,
Vijay
 
N

Nick Keighley

class aaaa
{
public:
int a;
public:
int getParentVar()
}

class bbb
{
public:
int b;
aaaa google;
}

int aaaa::getParentVar()
{
/* access class bbb's variable that is int b
}

so please tell me how to get int b of class bbb in the method
getParentVar of class aaa.

you can't there is no relationship between aaaa and bbb.
Also note bbb is *not* a "parent" of aaaa
 
S

SirMike

Dnia 5 Oct 2006 01:21:46 -0700, (e-mail address removed) napisa³(a):
so please tell me how to get int b of class bbb in the method
getParentVar of class aaa.

There is no possibility to do so. Hovewer, you can have a pointer to the
class bbbb in the class aaaa and initialize it in a constructor.
 
J

Jim Langston

Hi I have a simple problem

class aaaa
{
public:
int a;
public:
int getParentVar()
}

class bbb
{
public:
int b;
aaaa google;
}

int aaaa::getParentVar()
{
/* access class bbb's variable that is int b
}

so please tell me how to get int b of class bbb in the method
getParentVar of class aaa.

Thanks,
Vijay

aaaa and bbb are unrelated in any way, other than that bbb contains an
instance of aaaa.

However, the way I handle this is by passing to aaaa's constructor a pointer
to bbb as the parent and initialize it in the initialization list (giving me
a warning about passing self in constructor, but that's fine in this case).

class bbb;
class aaaa
{
public:
aaaa( bbb* parent ): Parent( parent );
int a;
int getParentVar() const;
private:
bbb* Parent;
};

class bbb
{
public:
bbb(): google( self ) {};
int b;
aaa google;
};

int aaaa::getParentVar()
{
return Parent->b;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top