Public Group

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.

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 on other sites
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 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 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 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 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.

• 13
• 18
• 29
• 11