Pure virtual method in a non abstract and a non interface class

S

Sharon

In my base call I need to write a pure virtual function in order to force the
derived classes to implement it.
The problem is that I cannot us an abstract method because the base class
itself cannot be abstract.
Also; a virtual function in not good enough, because the derived class may
not implement it.

So how can I write a pure virtual method in a non abstract and a non
interface class?
 
P

Patrice

The problem is that I cannot us an abstract method because the base class
itself cannot be abstract.

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 ?
 
A

Alberto Poblacion

Sharon said:
In my base call I need to write a pure virtual function in order to force
the
derived classes to implement it.
The problem is that I cannot us an abstract method because the base class
itself cannot be abstract.
Also; a virtual function in not good enough, because the derived class may
not implement it.

So how can I write a pure virtual method in a non abstract and a non
interface class?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top