I am trying to create a pure virtual base class (or simulated pure virtual)
my goal:
- User can't create instances of BaseClass.
- Derived classes have to implement default constructor, copy constructor, copy assignment operator and destructor.
My attempt:
class Base
{
public:
virtual ~Base() {};
/* some pure virtual functions */
private:
Base() = default;
Base(const Base& base) = default;
Base& operator=(const Base& base) = default;
}
This gives some errors complaining that (for one) the copy constructor is private. But i don't want this mimicked constructor to be called.
Can anyone give me the correct construction to do this if this is at all possible?
3 Answers 3
The requirement looks wrong. The reason is that virtual methods and copy constructors don't work together. Virtual methods are useful if you use the object polymorphically, that is via reference/pointer to a base class. But copy constructor always constructs object of the static type it is written as, so if you give it polymorphic object, it will only create the base class, not the subclass you wanted. This is called slicing.
So you want to either:
Have a templated framework that knows the type at compile time and uses copy-constructor. But this does not need any virtual methods, nor for that matter a common base type, since the code will be compiled with the specific template parameter. Compiler will complain if the type substituted does not support the operations the template tries to use, but you can slightly improve diagnostics by using some explicit checks, e.g. from Boost.Concept Check.
Have a framework using polymorphic objects with base class, but that can't create copies with copy constructor, because it does not know the type to construct. There are three ways out:
- Have a pure virtual
Base *clone()
method, that will be implemented to doreturn new Derived(this)
in each concrete subclass. You should probably wrap that inunique_ptr
orshared_ptr
immediately to avoid leaking the copies. - Instead of copies, hand out references or smart pointers, probably
shared_ptr
in this case. It would probably be either const references/smart pointers or typed with interface that only contains methods that can't affect invariants in the framework. - Make the method that inserts the objects a template, that would construct a cloner and store it along with the object. Cloner is a function
template<typename T> Base *clone(const Base *obj) { return new T(dynamic_cast<const T &>(*obj); }
(you can have a functor or a class with multiple helper methods like this). To ensure you really have correct type, the insert method should probably wrap the call tonew
as well similar to e.g.make_shared
.
- Have a pure virtual
By the way, nothing of this has anything to do with rule of three. Because rule of three says that if default copy constructor, default assignment operator or default destructor are not good enough for the class, than none of them is. But not because compiler would require it, but because the logic probably does. But for most classes that want to be copyable they are good enough. Because in most cases you hide the resource handling in some smart pointer and just let the default copy/assignment/destructor call to it.
-
2@JanHudec, there's a third solution that sits between the two you presented: type erasure. Using something like Boost.TypeErasure's "concepts-based polymorphism" through
any
, you can use concept constraints without having to use template functions everywhere.Bret Kuhns– Bret Kuhns2014年10月01日 13:16:52 +00:00Commented Oct 1, 2014 at 13:16
I am trying to create a pure virtual base class (or simulated pure virtual)
Sure:
User can't create instances of BaseClass.
Easy make the destructor virtual and pure
class Base
{
public:
virtual ~Base() = 0;
};
Base::~Base(){}
Now you can not instantiate a member of Base.
Derived classes have to implement default constructor, copy constructor, copy assignment operator and destructor.
You can't force any of that.
If the user does not provide any of the above the compiler will automatically generate public versions for you. But by making them private you have basically made any derived class non copyable (even if the create their own versions).
But in g++ there are some warnings to help you.
If you set -Weffc++ and also set -Werror It should generate a compile time error if you do not obey the rule of three.
-
Small note: Pure virtual destructor still needs implementationUldisK– UldisK2014年10月14日 08:43:00 +00:00Commented Oct 14, 2014 at 8:43
As the Rule of the Three says...
If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them.
In your example of abstract class with the inheritance hierarchy, you would have to make the destructor, copy constructor and the copy assignment operator your self. Do not declare either of them as private as you restrict their usage out of the class.
class Base
{
/* some pure virtual functions */
public:
Base() ;
Base(const Base& base) ;
Base& operator=(const Base& base) = default;
virtual ~Base() {};
} ;
As a matter of fact, assignment operator is not passed on in inheritance.
-
This does not prevent that classes can omit certain function implementationsMinion91– Minion912012年10月01日 09:38:06 +00:00Commented Oct 1, 2012 at 9:38
-
can you make your point clear? I am unable to get itCoding Mash– Coding Mash2012年10月01日 09:41:14 +00:00Commented Oct 1, 2012 at 9:41
-
I want to make sure that someone who uses my classes has to implement those 4 methodsMinion91– Minion912012年10月01日 09:43:11 +00:00Commented Oct 1, 2012 at 9:43
-
You mean you want that someone who derives the classes from
Base
creates implementations in the respective child classes as well?Coding Mash– Coding Mash2012年10月01日 09:44:47 +00:00Commented Oct 1, 2012 at 9:44 -
Explore related questions
See similar questions with these tags.
= delete
for that) or for all of them to restrict construction to a factory function. But such class is not inheritable, because the subclass constructor needs to call the base class constructor and it need to be at least protected for that. Making all constructors protected makes the class abstract.Base *clone()
that will be implemented asreturn new Derived(this)
in each subclass. The framework could with a bit of template glue generate "cloners" for the objects to avoid having to define that method manually if you have really many of them.