Is there any way for an interface class to enforce a definition of the copy constructor and maybe of also other constructors? In my case, I have an IResource
pure abstract class, and I want all classes that implement this interface to define a copy-constr, a constructor for loading from a file, and a constructor for loading from memory.
In order to construct an object, you need to know the concrete class to use (how would it otherwise know how much memory to allocate, or which virtual table to use, etc..?). As such, the interface is not in play when dealing with constructors, and you can't use interfaces (pure virtuals) to enforce the existence of such a constructor. It's kind of natural when you think about it, virtuals only work when you have a polymorphic object, i.e. after instantiation. Anyone referencing your IResource interface would only ever deal with instantiated objects, and never touch a constructor.
You can enforce these kind of constraints on stuff using templates if you want though. By simply calling the copy constructor from a templated function, the compiler will complain if it encounters a template instantiation using a type which does not have a copy constructor.
You cannot enforce that and it would not be a right way either. On the contrary, you should prevent the usage of public copy constructors in a polymorphic class hierarchy...
struct IResource {
virtual IResource* Clone() const = 0;
virtual ~IResource() {}
};
An implementer of IResource
should follow this pattern:
class ConcreteResource : public IResource, public boost::noncopyable { // or equivalent
public:
virtual ConcreteResource* Clone() const;
explicit ConcreteResource(std::string const & pString) : mString(pString) {}
private:
std::string mString;
};
ConcreteResource* ConcreteResource::Clone() const {
return new ConcreteResource(this->mString);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With