So how can I write a pure virtual method in a non abstract and a non
interface class?
--
Thanks
Sharon
Because ? Not sure but at first sigth it looks like to me that if a class
contains an abstract method then the class is necessarily abstract (what to
do when the abstract method is called ?)
You could perhaps have :
- an abstract class that provide the base implementation
- a concrete class that inherits from this and uses a "void" implementation
- others will derive from the same abstract base class
For now what we are missing is likely how you'll use this non abstract class
when you have an abstract method in it ?
--
Patrice
I don't think that it can be done in such a way that the compiler will
enforce it, but if you can settle for a run-time check, you can write a
virtual function in a non-abstract class, and inside the function just throw
a NotImplementedException. If the derived class does not override it, you
will get an error at runtime.
The real question is about the requirement that the base class not be
abstract. Given the conditions of what you are trying to accomplish, the
conclusion is that this base class *should* be abstract (and therefore the
method can be abstract and everything will behave as desired, so what is
your reasoning for claiming that "the base class itself cannot be abstract"?
You can always provide one abstract base class, and then inherit from it to
provide a concrete implementation with an empty method overriding the
abstract one if you require an instance of this class which doesn't use this
method.