473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

a parent and child class

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

Aug 19 '06 #1
4 3237
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11*********************@p79g2000cwp.googlegro ups.com...
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

Aug 19 '06 #2
In article <44**********************@newsspool1.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.dewrote:
>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
--
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?
Aug 19 '06 #3

Greg Comeau wrote:
In article <44**********************@newsspool1.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.dewrote:
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
--
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

Aug 19 '06 #4
In article <11*********************@p79g2000cwp.googlegroups. com>,
"markww" <ma****@gmail.comwrote:
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
}
};
Aug 19 '06 #5

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

Similar topics

14
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I...
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
6
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str;...
3
by: Peteroid | last post by:
Is it possible to make a public parent class method unavailable (i.e., generate an error at compile time) to a particular child class? For example, say a parent class has a public method Add( )....
4
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...
10
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...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
3
by: Eddie | last post by:
If FormMain = MDI parent, FormSub = Child parent, I execute FormSub from the menu like this way. FormSub^ sub = gcnew FormSub; sub->MdiParent = this; sub->Show(); This can generate child...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.