473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ child to Parent to child relationships

1 New Member
Hello, I am wondering if this is possible. If I have a parent class and a child that inherits the parent, the child can access a parents member function right? Well let's say we are in the parent member function that has just been accessed by a child. The program goes through some condition statements, comes true to a situation where it's true, and now needs to tell the child to do child member function x. Is that possible?

class parent{
doSomeChecks();
};

class child : public parent{
....
void xFunction();
void someFunction();
....
}
// =====================================
// the child function that calls a parent function doSomeChecks()
void child::someFunction(){
this->doSomeChecks();
}

// the parent function that has just been called by a child object.
void Parent::doSomeChecks(){

// this is where I want to call a child function from this parent class
// which has been called by a child object. Now how to call xFunction()?
}

// this function is the function I want to call from parent.
void child::xFunction(){
....
// do some work here
....
}

If anyone could shed some light on how and why this would or would not work would be super fantastic.
Thx.
Jan 31 '08 #1
2 6409
Laharl
849 Recognized Expert Contributor
In Python, you could do this with __class__, I'm not really sure that C++ has anything that performs a similar task builtin, though you might be able to work it through a method in each class that returns a String with the class name, and the base class would need access to the child class's methods, via friend classes.
Jan 31 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Yep. This is called the Hollywood Principle: Don't call us, we'll call you.

It is part of a design pattern called the Template Method, which you can research.

Here's how it works:

When a child object is created, it has a parent object embedded in it.

When the child calls a parent method, it is to the parent object that is embedded in the child. That parent object can then call a child method.

This works because the access specifiers public/private/protected are ignored when to comes to overriding.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. class Parent
  2. {
  3.     private:
  4.         virtual void MethodB() = 0;
  5.     public:
  6.         void MethodA();
  7.  
  8.  
  9. };
  10. void Parent::MethodA()
  11. {
  12.     this->MethodB();
  13. }
  14. class Child : public Parent
  15. {
  16.     private:
  17.         void MethodB();
  18. };
  19. void Child::MethodB()
  20. {
  21.     cout << "You have called Child::MethodB()" << endl;
  22. }
  23. int main()
  24. {
  25.      Parent* obj = new Child;
  26.      obj->MethodA();
  27. }
  28.  
Here the Child overrides the Parent MethodB. Then when a Child object is used with a Parent*, the call to Parent::MethodB is reallty a call to Child::MethodB because of the override.

The effect is a pointer to a Parent object has called a Child method, and in this case a private Child method since public/private/protected are ignored whe it comes to overriding.

The tecgnique is called separating the interface from the implementation. That is, the public Parent methods are the interface and the private Parent methods are the implementation. This leads to private virtual functions and this is considered today to be the correct way to design for polymorhism.
Jan 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bob C. | last post by:
I want to create a 1-many relationship. Parent table has a primary key, child table has no primary key. The child table does have an index with all four fields of the parent's PK. How can I do...
0
by: JJ | last post by:
Hi All, I am trying to Add nodes in code behind and am having problems with the parent child relationships. I get a Use of unassigned local variable error . I have the local variables defined...
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...
1
by: Mr Newbie | last post by:
Sorry to bother you guys but I have another questions related to DataSets. I'm almost there now, so I dont expect to bug you much more ( Hopefully ). I have Master / Details tables with...
0
by: Michael | last post by:
Okay, maybe I am missing something. I created a dataset through VS2005 data manager. The set includes tables with child relationships. The tables and the relationships are included in the dataset....
3
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...
1
by: adamredwards | last post by:
I have a page with some form elements that are dynamically generated. They are inserted into the dom by first cloning a node, changing the values like name, and then inserted with insertBefore(). ...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
8
by: Rick | last post by:
VS 2005 I' m setting up a parent/child datagridviews in a form. I am doing a lot of this by hand coding in order to get the feel of things. I want a change in the parent table to trigger a...
0
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,...
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
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.