I have a virtual base class function which should never be used in a particular derived class. Is there a way to \'delete\' it? I can of course just give it an empty definitio
It is not allowed by the standard, however you could use one of the following two workarounds to get a similar behaviour.
The first would be to use using
to change the visibility of the method to private, thus preventing others from using it. The problem with that solution is, that calling the method on a pointer of the super-class does not result in a compilation error.
class B
{
public:
virtual void f();
};
class D : public B
{
private:
using B::f;
};
The best solution I have found so far to get a compile-time error when calling D
s method is by using a static_assert
with a generic struct that inherits from false_type
. As long as noone ever calls the method, the struct stays undefied and the static_assert
won't fail.
If the method is called however, the struct is defined and its value is false, so the static_assert
fails.
If the method is not called, but you try to call it on a pointer of the super class, then D
s method is not defined and you get an undefined reference
compilation error.
template
struct fail : std::false_type
{
};
class B
{
public:
virtual void f()
{
}
};
class D : public B
{
public:
template
void
f()
{
static_assert (fail::value, "Do not use!");
}
};
Another workaround would be to throw an exception when the method is used, but that would only throw up on run-time.