468,512 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 5998
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,207 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

Post your reply

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

Similar topics

reply views Thread by JJ | last post: by
4 posts views Thread by Danny Tuppeny | last post: by
3 posts views Thread by reachsamdurai | last post: by
8 posts views Thread by Rick | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.