472,973 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

C++ child to Parent to child relationships

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 6372
Laharl
849 Expert 512MB
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 Expert Mod 8TB
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.