Archived

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

Virtual Function is VC++

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

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

Share this post


Link to post
Share on other sites
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 ):
    
class BaseClass {
public:
virtual void Output(void) {
printf("A");
}
};

class DeriClass : public BaseClass {
public:
virtual void Output(void) {
printf("B");
}
};

BaseClass Base;
DeriClass Deri;
BaseClass *PointerB = (BaseClass *) &Base;
BaseClass *PointerD = (BaseClass *) &Deri;

Base.Output();
Deri.Output();
PointerB->Output();
PointerD->Output();

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.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

Share this post


Link to post
Share on other sites
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:

  
CBaseClass* ptr = new CDerivedClass(someData); // this is legal

delete ptr; // this will call the CBaseClass destructor

// and if we don''t give this destructor the keyword

// virtual, the derived class'' destructor won''t get

// called.

[source]

Share this post


Link to post
Share on other sites
//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[i] = mytank;
}
}

for (i = 0; i < 10; i++)
Vehicles[i]->WhoAmI();

for (i = 0; i < 10; i++)
delete Vehicles[i];

}
//see ? now once it will call for the tank::clachit and then to the jeep:calchit
//i hope it helped you a bit to see its use
//ofcourse it''s not the ONLY use but i sure can say it''s the major one


Arkon
[QSoft Systems]

Share this post


Link to post
Share on other sites