Virtual Function is VC++
Hi all,
need a little help here, so....
Does anyone, can anyone explain briefly what
virtual functions are, and how to work with them,
maybe a short code example?
Thanks a zillion,
Hugo Ferreira
lotsjunk@hotmail.com
http://unitek3000.tripod.com
Virtual functions are member functions that can be overwritten by a function of the same name in an object that is derived from the class it is in. For example (excuse typos, I'm writing this fast
):
That would print out ABAB. They're pretty useful when used correctly. Of course, they can have a speed penalty if used incorrectly. [EDIT: I knew I'd make a typo...]
![Resist Windows XP's Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/ (If my website isn't down, it's a miracle!)
Edited by - Null and Void on June 12, 2001 1:06:23 AM
![](wink.gif)
|
That would print out ABAB. They're pretty useful when used correctly. Of course, they can have a speed penalty if used incorrectly. [EDIT: I knew I'd make a typo...]
![Resist Windows XP's Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/ (If my website isn't down, it's a miracle!)
Edited by - Null and Void on June 12, 2001 1:06:23 AM
The virtual keyword can also be used for destructors, and in this case it has a slightly different function.
When base class destructor is ''virtual'', it signals the compiler to call the derived class'' destructor first, if one exists. In this case, the function is not overridden, it is just called later.
This might be useful in the following situation:
When base class destructor is ''virtual'', it signals the compiler to call the derived class'' destructor first, if one exists. In this case, the function is not overridden, it is just called later.
This might be useful in the following situation:
|
//you wonder why to use virtual functions, don''t you?
//so i''ll try to help
#include
#include
#include
struct POINT{ int x, y; };
class _BaseVehicle
{
public:
POINT pos;
int power;
// etc
virtual void WhoAmI()
{
}
};
//now say you have a jeep and a tank
//so---->
class _Tank : public _BaseVehicle
{
void WhoAmI()
{
cout << endl << "I''m a tank!";
}
};
class _Jeep : public _BaseVehicle
{
void WhoAmI()
{
cout << endl << "I''m a jeep!";
}
};
//you see now instead of determining the type of class you can just call the CalcHit
//and the compiler will "know" to which CalcHit to call...
//you could do something like this:
void main()
{
clrscr();
_BaseVehicle *Vehicles[10];
for (int i = 0; i < 10; i++)
{
if (i & 1)
{
_Jeep *myjeep = new _Jeep;
Vehicles = myjeep;
}
else
{
_Tank *mytank = new _Tank;
Vehicles = mytank;<br> }<br> }<br><br>for (i = 0; i < 10; i++)<br> Vehicles->WhoAmI();<br><br>for (i = 0; i < 10; i++)<br> delete Vehicles;<br><br>}<br>//see ? now once it will call for the tank::clachit and then to the jeep:calchit<br>//i hope it helped you a bit to see its use<br>//ofcourse it''s not the ONLY use but i sure can say it''s the major one </i> <br><br><FONT COLOR="#80ff80">A</FONT><FONT COLOR="#9fff80">r</FONT><FONT COLOR="#bfff80">k</FONT><FONT COLOR="#dfff80">o</FONT><FONT COLOR="#ffff80">n</FONT><br>[<a href="http://qsoft.ragestorm.com"><FONT COLOR="#ff8040">Q</FONT><FONT COLOR="#e9754f">S</FONT><FONT COLOR="#d46a5f">o</FONT><FONT COLOR="#bf606f">f</FONT><FONT COLOR="#aa557f">t</FONT><FONT COLOR="#944a8f"> </FONT><FONT COLOR="#7f409f">S</FONT><FONT COLOR="#6a35af">y</FONT><FONT COLOR="#552abf">s</FONT><FONT COLOR="#3f20cf">t</FONT><FONT COLOR="#2a15df">e</FONT><FONT COLOR="#150aef">m</FONT><FONT COLOR="#0000ff">s</FONT></a>]
//so i''ll try to help
#include
#include
#include
struct POINT{ int x, y; };
class _BaseVehicle
{
public:
POINT pos;
int power;
// etc
virtual void WhoAmI()
{
}
};
//now say you have a jeep and a tank
//so---->
class _Tank : public _BaseVehicle
{
void WhoAmI()
{
cout << endl << "I''m a tank!";
}
};
class _Jeep : public _BaseVehicle
{
void WhoAmI()
{
cout << endl << "I''m a jeep!";
}
};
//you see now instead of determining the type of class you can just call the CalcHit
//and the compiler will "know" to which CalcHit to call...
//you could do something like this:
void main()
{
clrscr();
_BaseVehicle *Vehicles[10];
for (int i = 0; i < 10; i++)
{
if (i & 1)
{
_Jeep *myjeep = new _Jeep;
Vehicles = myjeep;
}
else
{
_Tank *mytank = new _Tank;
Vehicles = mytank;<br> }<br> }<br><br>for (i = 0; i < 10; i++)<br> Vehicles->WhoAmI();<br><br>for (i = 0; i < 10; i++)<br> delete Vehicles;<br><br>}<br>//see ? now once it will call for the tank::clachit and then to the jeep:calchit<br>//i hope it helped you a bit to see its use<br>//ofcourse it''s not the ONLY use but i sure can say it''s the major one </i> <br><br><FONT COLOR="#80ff80">A</FONT><FONT COLOR="#9fff80">r</FONT><FONT COLOR="#bfff80">k</FONT><FONT COLOR="#dfff80">o</FONT><FONT COLOR="#ffff80">n</FONT><br>[<a href="http://qsoft.ragestorm.com"><FONT COLOR="#ff8040">Q</FONT><FONT COLOR="#e9754f">S</FONT><FONT COLOR="#d46a5f">o</FONT><FONT COLOR="#bf606f">f</FONT><FONT COLOR="#aa557f">t</FONT><FONT COLOR="#944a8f"> </FONT><FONT COLOR="#7f409f">S</FONT><FONT COLOR="#6a35af">y</FONT><FONT COLOR="#552abf">s</FONT><FONT COLOR="#3f20cf">t</FONT><FONT COLOR="#2a15df">e</FONT><FONT COLOR="#150aef">m</FONT><FONT COLOR="#0000ff">s</FONT></a>]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement