Pure Virtual Functions

Hello all,

Say I have an inheritance tree, and I want to not be able to create objects of the class which is near the top of the tree. I could have a pure virtual function in the class, but what if I didn't want to redefine the function in all the derived classes ( a necessity for pure virtual functions) - is there any other way?

Thanks in advance for your replies.
You can declare constructors of this class as protected.

Another way is to declare the destructor as pure virtual and redefine it in derived classes.
Last edited on
All right, I will give that a go - Thanks Vlad.

I had a go at this, just wondering what you meant exactly, by this:
You can declare constructors of this class as protected.

Did you mean all the constructors, or just one.

Could you perhaps show a short example? Thanks for your help.
You can declare all constructors of a base class as protected. In this case they be called only from constructors of a derived class.
The protected constructor is a technique I use quite often. It sounds like it is exactly what you want.

If all constructors are protected, you can only create objects of derived classes with public constructors. Just make sure you have a virtual destructor (it doesn't have to be pure virtual in this situation).

I've never tried the pure virtual destructor option that Vlad suggested. It would probably work, but I think the protected constructor idea represents what you are trying to do better. It fits better semantically.
OK thanks Vlad & Doug4 - it works like a charm. I used the protected constructor solution.

Thanks Dudes !!
Topic archived. No new replies allowed.