Consider the following scenario: Class A defines many virtual functions. Class B and class C inherit from class A, but I want some virtual functions in class A to be available only in class B and not in class C.
I wrote the following code to try it out:
class A
{
public:
A() { }
virtual void play()
{
printf("A::play() \n");
}
};
class B : public A
{
public:
B() { }
virtual void play() = delete;
private:
};
class C : public A
{
public:
C() { }
virtual void play()
{
}
private:
};
The error is as follows: error C2282: "B::play" cannot be overwritten "A::play"
sure,we can do that like this:
class A
{
public:
A() { }
virtual void play()
{
printf("A::play() \n");
}
};
class B : public A
{
public:
B() { }
private:
virtual void play()
{
printf("This function has been removed.");
}
};
class C : public A
{
public:
C() { }
virtual void play()
{
}
private:
};
However, I don't think this implementation is elegant enough.(I want class B to have no play() function, even if it's private and no one else can use it)
So, i have two questions:
- How to elegantly implement the scenario I envisioned
- Can the delete keyword not be used for virtual functions? I can't find anything that says the delete keyword cannot be used for virtual functions