Why cant we create Object if constructor is in private section?

Posted by Abhi on Stack Overflow See other posts from Stack Overflow or by Abhi
Published on 2010-04-22T10:10:37Z Indexed on 2010/04/22 10:13 UTC
Read the original article Hit count: 173

Filed under:
|
|
|

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 cant we create object is my doubt...

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 only doubt is why cant we create object when constructor is in private section?

Thanks in advance

© Stack Overflow or respective owner

Related posts about c++

Related posts about constructor