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

Recommended Answers

All 2 Replies

Show your code.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.