Jump to content
  • Advertisement

Archived

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

programering

What do I use "virtual" for?

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

Advertisement
'virtual' makes sure that if you call that function on a pointer to a base type, the correct version of that function is called.

For example:


class Base
{
void NormalFunction(void)
{
std::cout << "Base::NormalFunction()" << endl;
}
virtual void VirtualFunction(void)
{
std::cout << "Base::VirtualFunction()" << endl;
}

virtual ~Base()
{
}
};

class Derived : public Base
{

void NormalFunction(void)
{
std::cout << "Derived::NormalFunction()" << endl;
}
virtual void VirtualFunction(void)
{
std::cout << "Derived::VirtualFunction()" << endl;
}
};

// TEST PROGRAM

int main(void)
{
Base* p = new Derived;

p->NormalFunction();
p->VirtualFunction();

delete p;

return 0;
}


Run this program, and the output should be:


Base::NormalFunction()
Derived::VirtualFunction()



[edited by - Sandman on September 23, 2003 7:29:32 AM]

Share this post


Link to post
Share on other sites
Thanks (Enter) Sandman! I think I understand!

The Base *p points to a Derived class instance,
so therefore virtual makes it run the right VirtualFunction of Derived class.

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Aleph One (Marathon Open Source) | My Homepage

Packa bajs med winzip!

Just dreaming wont make you (more) skilled in game programming/development.

Share this post


Link to post
Share on other sites
It's called late binding...

Example:

Classes:
Building
House : Building
School : Building

When you have a function that takes a pointer to a Building or one of it's children you can do the following thing:

void myFunc(Building*) { }

But... PROBLEM!!! What if a building and its children have a function printMyName()...
There is NO way the compiler can tell at compile-time of which object he should call the printMyName() function. The program has to figure this out under run-time with which object he's dealing.
The virtual keyword basically says that this function can be overridden by a child of this class, but it isn't sure at compile-time.



void myFunc(Building* building)
{
building.printMyName();
}


class Building
{
//parent class

virtual void printMyName();
};

class House : Building
{
//little kiddo

void printMyName();
};

class School : Building
{
//little big kiddo

void printMyName();
}


[edited by - Structural on September 23, 2003 12:06:27 PM]

Share this post


Link to post
Share on other sites

Structural:
void myFunc(Building* building){ building.printMyName();}
should be:
void myFunc(Building* building){ building->printMyName();}
or:
void myFunc(Building& building){ building.printMyName();}

I surely have understod virtual now!

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Aleph One (Marathon Open Source) | My Homepage

Packa bajs med winzip!

Just dreaming wont make you (more) skilled in game programming/development.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!