Sign in to follow this  

C++ Parent Class accessing Child functions, possible?

This topic is 2844 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
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[i]->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
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

This topic is 2844 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this