Polymorphism question

Started by
1 comment, last by sipickles 18 years ago
Hello, I have an unexpected development which has confused me. In my GUI, I have a class cBaseWindow. My button class cButton inherits from this. When buttons are made, pointers are stored in the GUI in a parent/child relationship. To render the button:
void cBaseWindow::Update( int x, int y, sGUIVertex* VertexBuffer )
{

	x += m_x;
	y += m_y;

	if ( m_permissions.m_isVisible )
	{
		// render this window
		AddToVB( x, y, VertexBuffer );
	}

	// render the children
	int numChildren = m_children.size();
	for ( int i = numChildren - 1; i >= 0; i--)
		m_children->Update( x, y, VertexBuffer );

}
This works fine for cButtons. AddToVB() is overridden by cButton in a polymorphic way. Now, I have another class, cTextButton. This inherits from cButton and has support for text labelling ( as opposed to an icon, which is cIconButton ). When I now call cBaseWindow::AddToVB(), the cTextButton calls the cButton::AddToVB() version, not the cTextButton::AddToVB() Why isnt cTextButton::AddToVB() called instead? Any ideas gratefully received... Thanks Simon
Advertisement
Did you use the virtual keyword for your function declaration?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Argh!

Thanks for guessing the obvious!

:)

This topic is closed to new replies.

Advertisement