Jump to content

virtual keyword in c++


cassius
 Share

Recommended Posts

I have been looking up the meaning of this keyword " virtual" and although theres no shortage of explanations I am too thick to understand any of them.

I know it can be used with classes or functions though.

 

Anyone willing to take a shot at explaining this keyword??

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

virtual is very important

 

I'm not too technical on this but it comes into play when you have inherited objects I'll try to use it in a game example:

 

 

Say I have the base class object with a function tostring() and declared as a virtual function.

 

class baseCamera

{

virtual string tostring()

}

 

now I have a derived classes which overrides the tostring method of the base class.

 

class fpsCamera: public baseCamera

{

string tostring();

}

 

class tpsCamera: public baseCamera

{

string tostring();

}

 

 

Say in my game world I always want to reference the current camera, I would use a baseCamera pointer as I can point to either the fps and tps camera (whichever is the current)

 

class GameWorld

{

baseCamera *cam;

}

 

now I want to call cam->tostring() I don't care if it's tps or fps and I don't want to call the baseCamera's tostring method I want to call the tpsCameras tostring() method or the fpsCamera's tostring() and that's what adding virtual will do for you. It will call the derived classes method first.

 

Hope I explained it good enough

trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Link to comment
Share on other sites

class animal{
virtual void makenoise() { std::cout << "??" << std::endl; }
}

class dog: public animal{
void makenoise() { std::cout << "wof!" << std::endl; }
}


std::vector<animal> animals;

 

 

 

 

 

now if you iter over your animals vector dogs says woof, with out the virtual keyword they would say ?? even though they define their own makenoise() command - you would have to downcast them to dogs ( and know to do this ) to make them say woof.

 

 

adding

 

class bigDog: public dog{
void makenoise() { std::cout << "big wof!" << std::endl; }
}

 

 

to the above example big doges will go "big wof!" even though dog is missing the virtual keyword - this is because virtual functions will always stay virtual. For this reason it's concidered poilite to type it out anyway when you overload a virtual function, even if you dont need to. It just avoids confusion

System:

Linux Mint 17 ( = Ubuntu 14.04 with cinnamon desktop ) Ubuntu 14.04, AMD HD 6850, i5 2500k

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...