deleyd
asked on
C++ inheritance, virtual, override, syntax
I need an overview of the syntax for doing this in C++. I have a BaseClass, and an ExtendedClass which inherits the BaseClass.
In the BaseClass I define a function foobar. In the ExtendedClass I may or may not want to override that function foobar providing alternate code for it to do.
If my ExtendedClass does not override foobar, then I want a call to foobar to run the BaseClass foobar.
If my ExtendedClass does override foobar, then I want a call to foobar to run the ExtendedClass foobar.
Sounds easy enough. I just need an example, as I get all tangled up in where to put the virtual keyword, and/or the override keyword. ('overrides'? 'overrideable'?)
Currently if I say virtual foobar in the BaseClass.h file, then I have to add virtual foobar in the ExtendedClass.h file if I want to override it, plus add the actual code in the ExtendedClass.cpp file. So I have to change two places, make sure they are in sync, and I don't like that; unless that's the way it's supposed to be for C++. I'm more of a C# programmer but this project requires C++.
In the BaseClass I define a function foobar. In the ExtendedClass I may or may not want to override that function foobar providing alternate code for it to do.
If my ExtendedClass does not override foobar, then I want a call to foobar to run the BaseClass foobar.
If my ExtendedClass does override foobar, then I want a call to foobar to run the ExtendedClass foobar.
Sounds easy enough. I just need an example, as I get all tangled up in where to put the virtual keyword, and/or the override keyword. ('overrides'? 'overrideable'?)
Currently if I say virtual foobar in the BaseClass.h file, then I have to add virtual foobar in the ExtendedClass.h file if I want to override it, plus add the actual code in the ExtendedClass.cpp file. So I have to change two places, make sure they are in sync, and I don't like that; unless that's the way it's supposed to be for C++. I'm more of a C# programmer but this project requires C++.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window