Jump to content
  • Advertisement
Sign in to follow this  
boxrick

C++ Parent Class accessing Child functions, possible?

This topic is 3211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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~?

Share this post


Link to post
Share on other sites
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();
}




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
But if i use the above then you can never create an instance of the parent ? It is simply there to be inherited from?

Share this post


Link to post
Share on other sites
Provide a definition for the pure virtual functions.

Change:

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



To:


virtual void DoStuffImpl() { }
virtual void Foobar() { }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!