1

Consider the following example.

class Parent
{
public:
    Child createChild();

// some member data and functions can go here.
}

class Child: public Parent
{
// some member data and functions can go here.
}

I would like to allow creation of "Child" class only through the method provided in the Parent class. That is I would like to deny the options to the user to instantiate an object of the Child class. I would also like avoid all the other default constructions of the Child class. How is it possible?

Parent p;
Child c = p.createChild(); // should be possible
Child d; //should not be allowed
Child e = c; // may  be allowed
7
  • 2
    Impossible. There's no way Child c = p.createChild(); can be allowed to work while Child e = c; may not. Your demands don't make much sense to me. (Unless you intend to make your classes move-only, and Child e = std::move(c); may work, is that the case?) Commented Apr 25, 2018 at 11:38
  • @StoryTeller wouldn't c++17 guaranteed copy elision remove the need for the copy/move constructors?
    – Mgetz
    Commented Apr 25, 2018 at 11:45
  • @Mgetz - Indeed. But this was tagged c++11. If it's a mistagging my comment is irrelevant. Commented Apr 25, 2018 at 11:51
  • @StoryTeller: You are right, it may be impossible if we don't allow Child e=c. So editing my question.
    – Sooraj
    Commented Apr 25, 2018 at 11:56
  • @StoryTeller actually I think it's incredibly relevant as that's a major change in behavior as far as the answer is concerned. Since this is possible in c++17 without quirks and impossible in C++11 without making the object movable or copyable.
    – Mgetz
    Commented Apr 25, 2018 at 12:44

3 Answers 3

6

You can make the Child constructor private, and have Parent::createChild be a friend of Child.


Do note the comment by StoryTeller, in that your copy-constructor still needs to be public.

0

You can do something like this.

class Parent
{
public:
      static Parent* InstantiateParent(); // for completeness' sake
      static Child* InstantiateChild();
};

class Child : public Parent
{
    Child();
    Child(Child& other):
    Child& operator = (Child& other);

    friend class Parent;
public:
};

/* impl.cpp */

#include "header.hpp";

Child* Parent::InstantiateChild()
{
    return new Child;
}

Parent* Parent::InstantiateParent()
{
    return new Parent;
}

Although, separating creation from the target class through some variation of factory pattern has its own advantages.

0
0

You could have CreateChild() return something other than a Child, which is publicly move-constructable to a Child. You pay for the move, because it isn't copy elided, and the code is redundant come c++17.

Could you live with taking a const or right reference to the temporary, which will extend its lifetime? I don't think there are any cases where it wouldn't have the same lifetime as the uncopyable object created directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.