a parent and child class

M

markww

Hi,

I have a two classes setup like this:

class CChild {
void Something();
};

class CParent {
string m_strData;
CChild m_Child;
};

Is there anyway that the cchild class member can get at m_strData in
the parent like:

void CChild::Something()
{
GetParentClass()->m_strData;
}

Thanks for any ideas
 
H

Heinz Ozwirk

markww said:
Hi,

I have a two classes setup like this:

class CChild {
void Something();
};

class CParent {
string m_strData;
CChild m_Child;
};

Is there anyway that the cchild class member can get at m_strData in
the parent like:

void CChild::Something()
{
GetParentClass()->m_strData;
}

An instance of the CParent class can pass a pointer to itself to m_Child,
for example in its constructor. The child saves this pointer and can use it
to access its parent. If the child can exist longer than its parent (not if
the child is a member of its parent class), the parent should notify all its
children that is it about to be destroyed. That would best be done in the
parent's destructor.

And of cause, the client class can only access public members of the parent
class (unless it is a friend).

HTH
Heinz
 
G

Greg Comeau

And of cause, the client class can only access public members of the parent
class (unless it is a friend).

....by identifer name. That is:

"The C++ access control mechanisms provide protection against accident -
not against fraud" -- Stroustrup
 
M

markww

Greg said:
...by identifer name. That is:

"The C++ access control mechanisms provide protection against accident -
not against fraud" -- Stroustrup
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

That is terrific, thanks guys,

Mark
 
D

Daniel T.

"markww said:
Hi,

I have a two classes setup like this:

class CChild {
void Something();
};

class CParent {
string m_strData;
CChild m_Child;
};

Is there anyway that the cchild class member can get at m_strData in
the parent like:

void CChild::Something()
{
GetParentClass()->m_strData;
}

Thanks for any ideas

class CChild {
public:
void Something( CParent* parent ) {
parent->m_strData; // assumes m_strData is public
}
};
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top