Search code examples
c++visual-studio-2010instantiationstatic-constructor

Object Instantiation in c ++ with a protected constructor


I have this c++ class and I want to initialize an object of this type:

class MyClass

{
public:

   /**
     *  Creates an instance of this class.
     *  @return Pointer to the created object.
     */    
static MyClass * Create ();


protected:
    // Explicit protected Constructor 
    //and Copy-Constructor, use Create() to create an  instance of this object.
    MyClass();

}

To create an instance, I did this:

static MyClass * m_object = myClass.Create();

but I got those warnings and errors:

   warning C4832: token '.' is illegal after UDT 'MyClass'

   error C2275: 'MyClass' : illegal use of this type as an expression

   error C2228: left of '.Create' must have class/struct/union

How to instantiate properly this object?


Solution

  • In C++, Static variables/methods are access using scope resolution (::) operator.

    change your code to

    static MyClass * m_object = MyClass::Create();