Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » |SOLVED] Vector of object: cast to inherited class
Re: Vector of object: cast to inherited class [message #52364 is a reply to message #52363] Sat, 14 September 2019 21:39 Go to previous messageGo to previous message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
Check how code below works.
A hint: I'm not using a keyword class. I'm using struct instead. This makes code shorter and cleaner.
struct A {
	A() {}
	virtual ~A() {}
	virtual void Hello() const {
		Cout() << "Hello from A" << EOL;
	}
};

struct B : A {
	B() {}
	~B() {}
	void Hello() const {
		Cout() << "Hello from B" << EOL;
	}
};

CONSOLE_APP_MAIN
{
	Array<A> arrA;
	arrA.Create<B>();
	arrA.Add(new B);
	arrA.Add();
	for (const A& a: arrA)
		a.Hello();
}


Regards,
Novo
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: copying code from web into IDE
Next Topic: Simple way to develope 2D Game
Goto Forum:
  


Current Time: Sun Apr 28 22:34:50 CEST 2024

Total time taken to generate the page: 2.02203 seconds