views:

277

answers:

4

Dear all

I want to know why cant we create object if the constructor is in private section. I know that if i make a method static i can call that method using

<classname> :: <methodname(...)>;

But why can't we create object is what I don't understand.

I also know if my method is not static then also I can call function by the following:

class A
{
     A();
     public:
        void fun1();
        void fun2();
        void fun3();
};


int main()
{
     A *obj =(A*)malloc(sizeof(A));
     //Here we can't use new A() because constructor is in private 
     //but we can use malloc with it, but it will not call the constructor
     //and hence it is harmful because object may not be in usable state.
     obj->fun1();
     obj->fun2();
     obj->fun3();
}

So, my question is: why can't we create an object when constructor is private?

+8  A: 

Because it is not accessible to the program, that's what private means. If you declared a member function or variable private, you would not be able to access them either. Creating private constructors is actually a useful technique in C++, as it allows you to say that only specific classes can create instances of the type. For example:

class A {
   A() {} // private ctor
   friend class B;
};

class B {
   public:
     A * MakeA() {
        return new A;
     }
};

Only B can create A objects - this is useful when implementing the factory pattern.

anon
Any other way by which i can create an object not using malloc. As using malloc , object may not be in usable state.
Abhi
@Abhi Make the constructor public
Andreas Brinck
@Abhi See above.
anon
@Neil Butterworth:Thank you very much. I was in search of this answer. Thank u friend....
Abhi
@Andreas Brinck : If i have to do so. I would not post my doubt in stack overflow. The question was if constructor is private then how to create object without using malloc.
Abhi
@Ahbi: If you want to keep you constructor private and still be able to instantiate your class, you'll need to implement a static factory method. I'll post a response that shows how to do this.
John Dibling
+2  A: 

A constructor is a special member function. It obeys the same rules as any other method when it comes to accessing it. The private access label prevents class users from invoking/accessing members declared under it.

shadeMe
+2  A: 

The "new" operator needs to call the constructor, so if the constructor is private you can not execute the code "obj = new A" except inside member functions of the class A itself.

I would guess that what you have encountered is a technique that is very often used in Java (and yes I know you're writing C++, but the principle is the same) where the designer of the class wants to make sure that one and only one instance of this class will ever exist (which is called a "singleton"). To achieve this, he needs to prevent other code from creating further instances of the class using new, and making the constructor private is one way to do that. Here's a piece of Java code illustrating the technique.

public class MySingleton {

  private MySingleton() {
      // Private constructor, to prevent instantiation using "new"
      // outside of this class.
  }

  public synchronized static MySingleton getInstance() {
    static MySingleton instance = null;

    if (instance == null) {
        // I can use new here because I'm inside the class.
        instance = new MySingleton();
    }
    return instance;
  }

}

Even if you don't know Java, the syntax is similar enough to C++ that you should understand what this code is doing. The point is that the only way to get a reference to an instance of the MySingleton class elsewhere in the code is to call the static class member getInstance().

  MySingleton obj = MySingleton.getInstance();
joefis
+1  A: 

You can't instantiate your class because the constructor is private. private member variables and functions cannot be used outside of the class itself.

If you want to be able to instantiate your class, you have two options.

Option 1 is to make the constructor. This is the Right Thing to do in the vast majority of cases. Making a constructor private is a useful technique, but only when trying to accomplish specific goals.

Option 2 is to create a public static factory method. You would typically do this when Option 1 isn't an option.

class A
{
     A();
     public:
    static A* Create() { return new A; }
        void fun1();
        void fun2();
        void fun3();
};


int main()
{
     A *obj = A::Create();
     //Here we can't use new A() because constructor is in private 
     //but we can use malloc with it, but it will not call the constructor
     //and hence it is harmful because object may not be in usable state.
     obj->fun1();
     obj->fun2();
     obj->fun3();
}
John Dibling
@John Dibling: Thank you friend
Abhi