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: |SOLVED] Vector of object: cast to inherited class [message #52390 is a reply to message #52353] Tue, 17 September 2019 09:55 Go to previous messageGo to previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
OK, I got it, If we don't define Hello() as Virtual then if we do this kind of thing :
	B b;
	A* a = &b;
	a->Hello(); //Printing "Hello from A"

Since Hello() is static defined C++ will only use type of declared variable to know what function he must call.
if "a" was B* then we would have call B::Hello() etc...
However, if we set Hello() as virtual in A and redifine it in B then the function C++ must call will not be define at compilation but during the runtime, for each call of Hello() C++ will test the real type of Object (I don't know how he do it) then call the right definition (based on type of object) of the function.
	B b; 
	A* a = &b; //here Hello() is virtual in A  and redefined in B
	a->Hello();//C++ try to know real type of "a" and see it's a B object (or maybe a B* even if I store it in A*) then Printing "Hello from B"

Very Happy Very Happy
 
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: Tue May 13 20:58:33 CEST 2025

Total time taken to generate the page: 0.00488 seconds