Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

while1

std::vector and OOP

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

Please look at this code:
      

class Window
{
public:
	Window() {}
	virtual ~Window()  {}
	virtual void Print() { cout << "Window" << endl; }
};

class Control : public Window
{
public:
	Control() {}
	virtual ~Control() {}
	virtual void Print() { cout << "Control" << endl; }
};

class Button : public Control
{
public:
	Button() {}
	virtual ~Button() {}
	void Print() { cout << "Button" << endl; }
};

class TextBox : public Control
{
public:
	TextBox() {}
	virtual ~TextBox() {}
	void Print() { cout << "TextBox" << endl; }
};

vector<Control*> m_aControls;

int main()
{
	// Create a new Button control

	Button* pButton = new Button;
	m_aControls.push_back(pButton);

	// Create a new textbox control

	TextBox* pTextBox = new TextBox;
	m_aControls.push_back(pTextBox);

	for(int x=0; x<m_aControls.size(); x++)
	{
		// I want to test if(m_aControls[x] == Button)

		m_aControls[x]->Print(); 
	}

	return 0;
}

      
How can I know in advance if the vector object m_aControls[x] is a Button or a TextBox? Edited by - while1 on February 23, 2002 3:59:22 PM Edited by - while1 on February 23, 2002 4:00:17 PM

Share this post


Link to post
Share on other sites
Advertisement
A quick note: RTTI must be explicitly enabled in VC++ or it'll throw up on you (I think Borland has it enabled by default). If you're using VC++ then have a browse around the project settings for an "Enable RTTI" option (or similar).

EDIT - oops! The previous poster said that.

Alimonster

There are no stupid questions, but there are a lot of inquisitive idiots.

Edited by - Alimonster on February 23, 2002 4:57:40 PM

Share this post


Link to post
Share on other sites

If you are REALLY using Object-Oriented Programming, then,
you would **NOT** need to know if a Control * points to
a button or a text box. That is the WHOLE point of
using polymorphism... afterall.


Premature optimizations can only slow down your project even more.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!