Jump to content
  • Advertisement
Sign in to follow this  
Telastyn

C++ Inherited function question

This topic is 5405 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

Running through some inheritance learning, I've come across a problem that seems as though it might have a somewhat arcane solution. When a derived class is created and destroyed it will call the constructors and the destructors of the classes it's derived from. Is there any way of having other functions behave similarly, perhaps akin to the constructor's ability to use d(int a): base(a){} ?

Share this post


Link to post
Share on other sites
Advertisement
Do you mean in this scenario:

class A
{
public:
virtual void Function()
{
cout<<"Foo";
}
};

class B : public A
{
public:
void Function()
{
// To call A's Function()
A::Function();
cout<<"Bar"<<endl;
}
};

int main()
{
B c;
c.Function();
return 0;
}



Hope that helps

Share this post


Link to post
Share on other sites
Sort of?



class A
{
int aint;
public:
virtual void Function()
{
cout<< aint;
}
};

class B : public A
{
int bint;
public:
void Function()
{
// To call A's Function()
(A *)this->Function();
cout<<","<<bint<<endl;
}
};

int main()
{
B c;
c.Function();
return 0;
}



Except that doesn't work.

I could give Function different names, but that kinda defeats the purpose.

I imagine this is perhaps just one of those cases where a slight design change would solve the problem better than programatic gymnastics.

Just for reference, this problem came up when dealing with GUI classes. I'd like each GUI class to register keybindings for itself. A scrolling list for example will need to bind the arrow keys in addition to whatever a common list binds. Should it lose and regain focus, it would need to regenerate those bindings.

I could simply destroy and recreate the object, or perhaps store the keybindings with the objects and link to them in the input stack instead... Anyways, it'd still be nice to know if such a thing was possible at all.

Share this post


Link to post
Share on other sites
umm... Did Programmer16's solution not work. It looked liked that was the way to do it to me(yes, it's possible).

Share this post


Link to post
Share on other sites
The problem with what you're doing is that you're converting B to A, but you're still calling B's function. You have to use A::Function() to call it.


class A
{
int aint;
public:
virtual void Function()
{
cout<< aint;
}
};

class B : public A
{
int bint;
public:
void Function()
{
// To call A's Function()
A::Function();
cout<<","<<bint<<endl;
}
};

int main()
{
B c;
c.Function();
return 0;
}

Which is effectively the same as:

class A
{
int aint;
public:
virtual void Function()
{
cout<< aint;
}
};

class B : public A
{
int bint;
public:
void Function()
{
// To call A's Function()
A NewA;
NewA.Function();
cout<<","<<bint<<endl;
}
};

int main()
{
B c;
c.Function();
return 0;
}


Share this post


Link to post
Share on other sites
Ah, okay. I was incorrectly thinking A::Function() wouldn't know what context it was in...

Doing it that way does work, I'll poke around at it to see if it's still unweildy enough for redesign.

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!