cassius Posted September 14, 2014 Share Posted September 14, 2014 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?? Quote 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 More sharing options...
tjheldna Posted September 15, 2014 Share Posted September 15, 2014 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 Quote Link to comment Share on other sites More sharing options...
Guppy Posted September 15, 2014 Share Posted September 15, 2014 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 Quote 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 More sharing options...
cassius Posted September 15, 2014 Author Share Posted September 15, 2014 Thanks fellas I think its sinking in. Quote 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 More sharing options...
Recommended Posts
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.