Sign in to follow this  

the virtual keyword?

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

virtual allows you to override functions, and then if you have a pointer to the base class, aka:


class A
{
...
};
class B : public A
{
...
};

A * ptr = new B; //<--- this


Calling a virtual function via this pointer:

ptr->call_function();


will call the one that B overwrote it with, but if it is not virtual, it will call the original one that A has.

Automatically making destructors virtual is a safety measure, so if you do this:

delete ptr;


on the above code, it will clean everything up correctly. Otherwise, it would only call class A's destructor, not B's, even though the thing being pointed to is indeed the class B.

Next time, you might want to try google:

http://www.google.com/search?q=keyword+virtual+C%2B%2B&sourceid=firefox&start=0&start=0&ie=utf-8&oe=utf-8

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
[...]
Next time, you might want to try google:

http://www.google.com/search?q=keyword+virtual+C%2B%2B&sourceid=firefox&start=0&start=0&ie=utf-8&oe=utf-8


I never find anything when I do that lol.. but thanks!:D

Share this post


Link to post
Share on other sites
Quote:
Original post by sushi-oneI never find anything when I do that lol..

then you need to practice, and google until you get really fustrated, to help lean what keywords work and which ones don't. Hint: putting things in quotes will make google look for that phrase. Aka, googling:

My Fat Cat

can return return results like:

My brother is trying to cut down on his fat by chasing his cat around the back yard.

Whereas:

"My Fat Cat"

will return way too many personal homepages of people who have nothing better to do than write about their fat cats.

You could also try googling "Searching google".

Share this post


Link to post
Share on other sites
Quote:
Original post by sushi-one
When Visual Studio creates classes for me, it puts "virtual" infront of the Class's destructor.. why use "virtual"?:)


Are you asking what virtual means or why in particular it is being used with destructors? Or something else? (c:

Share this post


Link to post
Share on other sites

This topic is 4836 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this