I have designed a class which uses overloaded member functions that call each other such that functions with additional arguments modify member variables that the original function relies on. It works as expected/intended when inheritance is not involved, but I found myself reusing the code often enough to want to create an abstract class.
I did this by having the original member function exit with an error in the abstract class, but still implementing the overloaded functions that modify member variables before calling the original function. This is done with the intent of only having to override the original function in my derived classes. However, when I create a derived class from this abstract class and supposedly override the member function, the functions that are not overridden still seem to be using the abstract class's definition of the member function-- the one that exits with an error. Here is a minimal example that causes the unexpected behavior.
#include <iostream>
#include <string>
#include <cstdlib>
class Base
{
// this class exists only to be derived from
public:
void write_message()
{
std::cerr << "Base is an abstract class not intended for direct use in"
<< " programs." << std::endl;
exit(1);
}
void write_message(std::string NewMessage)
{
Message = NewMessage;
write_message();
}
protected:
std::string Message = "This is the default message";
};
class Derived : public Base
{
public:
using Base::write_message;
void write_message()
{
std::cout << Message << std::endl;
}
private:
// intentionally blank
};
int main() {
Derived MyObject;
MyObject.write_message("Here is a modified message");
return 0;
}
As I'm sure is clear from this example, I am already aware of the common pitfall involving inheriting overloaded member functions which is topic of other questions on this site, which is needing to have using Base::write_message
in my derived class. A couple of those questions include: this and this. What those questions don't address is this issue involving the overriding of write_message
not working the way I expect it to.
What is missing here? I can't reason out why Base::write_message()
would be used of Derived::write_message
when calling MyObject.write_message("Here is a modified message");
in the code above.
I am using cpp17 and g++12.2.1 on linux.