U++ framework
Do not panic. Ask here before giving up.

Home » Community » Newbie corner » |SOLVED] Vector of object: cast to inherited class
Re: |SOLVED] Vector of object: cast to inherited class [message #52389 is a reply to message #52387] Mon, 16 September 2019 23:47 Go to previous messageGo to previous message
Novo is currently offline  Novo
Messages: 1432
Registered: December 2006
Ultimate Contributor
Xemuth wrote on Mon, 16 September 2019 14:25
Define Hello() as virtual on A allow us to redefine Hello() on Children but since B destructor will call A destructor at is exit and
A don't know if child have possible redefinition of Hello() he will call is own Hello() declaration. Very Happy

Still wrong. In case of "a->Hello();" a doesn't know about possible redefinition of Hello() as well, but it still prints "Hello from B".

And destructor of B doesn't call destructor of A. It works in a different way, although the order of calls is the same.

And you do not have to declare Hello() in A as virtual if you want to redefine it in B. Code below will compile.
struct A {
	virtual ~A() {
		Hello();
	}
	void Hello() const {
		Cout() << "Hello from A" << EOL;
	}
};

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



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: Thu Jun 18 08:10:16 GMT+2 2026

Total time taken to generate the page: 0.00758 seconds