Usage of virtual functions

Are virtual functions only used for base class pointers or references to classes that are derived from that base class?

The pointers/references can also point to objects of the base class.
What do you mean by
...only used for...
? That tells me that you might be missing how powerful of a tool these are. Tell me, can you think of a practical use for them? Or do they seem like one of those things you feel like you'll never use?

If they point to the base class, does the virtual matter?


That's not what I am trying to get to. I am trying to ask if they are only used with references or pointers. For example, if you just instance a regular base class and call a virtual function, the virtual does not really matter.
Last edited on
Correct, if you are just dealing with the object it self and not with a reference to the base class then the virtual tag doesn't really matter except that it allows you to reuse names for functions that have inherited from objects that already have functions of the same name.
If they point to the base class, does the virtual matter?

If you know that the pointer points to the base class it would behave the same as if you declared the function without the virtual keyword. The strength with inheritance and virtual functions is that you can write code without knowing the exact type of the object the pointer points to. It could be the base class or some of the derived classes.
Topic archived. No new replies allowed.