C++ Parent Class accessing Child functions, possible?

Started by
5 comments, last by Zahlman 14 years, 1 month ago
This is probably terrible design and totally pointless but is the following possible: I have an instance of a class lets call it "Parent" and "Child" is the child of "Parent". Is it possible for "Parent" to access "Child" member functions, if so then how? If i shouldn't be using this method what would be a better implementation~?
Advertisement
Quote:Original post by boxrick
I have an instance of a class lets call it "Parent" and "Child" is the child of "Parent". Is it possible for "Parent" to access "Child" member functions, if so then how?


Yes, you can; through questionable trickery of down-casting. This generally suggests that the overall design is flawed somewhere.

Quote:
If i shouldn't be using this method what would be a better implementation~?


Design it so that you fall on a virtual method instead:

#include <iostream>#include <cstdlib>using namespace std;class A {public:	virtual ~A() {	}	virtual void DoStuffImpl() = 0;	virtual void Foobar() = 0;	void DoStuff() {		cout << "In A::DoStuff() .. ";		if ( rand()%2 )			this->DoStuffImpl();				else			this->Foobar();	}};class B : public A {public:	void DoStuffImpl() {		cout << "A::DoStuffImpl()\n";	}	void Foobar() {		cout << "A::Foobar()\n";	}};class C : public A {public:	void DoStuffImpl() {		cout << "B::DoStuffImpl()\n";	}		void Foobar() {		cout << "B::Foobar()\n";	}};int main() {	B b;	C c;	A* as[] = { &b, &c };	for ( unsigned i = 0; i < 2; ++i )		as->DoStuff();}
It sounds like you want virtual functions:

class Parent{   virtual void Foo()   {      cout << "Parent::Foo()";   }   void Bar()   {      Foo();   }};class Child{   virtual void Foo()   {      //Parent::Foo();      cout << "Child::Foo()";   }};int main(int argc, char** argv){   Child* c = new Child();   c->Bar();}


will output

Child::Foo()

If you uncomment the the commented line, it will output

Parent::Foo()
Child::Foo()

Basically, Parent has access to a function implemented in a child class.

If you're using some different definition of parent/child then the answer might be different, but this is how I interpreted your question.
But if i use the above then you can never create an instance of the parent ? It is simply there to be inherited from?
Provide a definition for the pure virtual functions.

Change:
virtual void DoStuffImpl() = 0;virtual void Foobar() = 0;


To:
virtual void DoStuffImpl() { }virtual void Foobar() { }
Quote:Original post by boxrick
But if i use the above then you can never create an instance of the parent ? It is simply there to be inherited from?


You can create instances of the parent. If your virtual function has =0 at the end of it then you can't. For example:

class Parent{public:   virtual void Foo() = 0;};


Now you can't create an instance of Parent. But in the example I gave above you can. Which function gets called at runtime depends on what type you created.

If you created an instance of Parent, Parent::Foo() will be invoked. If you created an instance of Child, Child::Foo() will be invoked. If you create an instance of Child, but in the definition of Child you didn't provide a new definition for the function Foo(), then Parent::Foo() will be invoked.

Basically, the function invoked is that of the MOST DERIVED class which provides an implementation.

If you want to do something more specialized than that, then it's probably a bad design.
Give a real example of why you think you want to be able to do this.

This topic is closed to new replies.

Advertisement