0

There is a pure virtual const function declared like wayyyy up the inheritance hierarchy that I need to override, but I would like to override it with a non-const function. I tried and it just complained that the pure virtual const function const was not implemented. I got around it for now by making the member that I needed to change 'mutable', but everyone is telling me that is a terrible idea. What else could I do?

Thanks,

David

2
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by daviddoria
0

It's basically impossible to show here - it is part of a giant library. Here is a mini-version - I'm not sure that it's any more helpful than the explanation in the original post:

#include <iostream>

class A 
{ 
	virtual void MyFunc() const = 0; 
}; 

class B : public A 
{ 
	virtual void MyFunc() const = 0; 
}; 

class C : public B 
{ 
	private:
		mutable int MyVar;
		
	public:
	void MyFunc() const
	{
		std::cout << "hello world";
		MyVar = 2;
	}
}; 

int main()
{
	C MyC;
	MyC.MyFunc();
	return 0;
}

To set MyVar to 2 in C::MyFunc, I had to make C::MyVar mutable. What I would have liked to is declare MyFunc non-const, but I cannot change the const-ness of A and B.

Dave

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.