Error: expected unqualified-id before '. sign

class A
{
    private:
        A () {}

    public:
        static A* getInstance ()
        {
            return new A ();
        }
};

int main ()
{
    A.getInstance ();
    return 0;
}

results in the error indicated in the header. I really understand that if I create a variable in class A and initialize it there and return it directly, the error will disappear.

But here I want to understand what is the meaning of this error and why I can not use it in this way.

+3
source share
5 answers

You need to call the method using the scope resolution operator - :::

 A::getInstance ();

Also, if that means it's singleton, it's very bad. Whenever you call getInstance(), you will get a new object and you will encounter memory leaks if you forget to delete all instances.

The syntax is usually done like this:

class A
{
    private:
        A () {}
        static A* instance;
    public:
        static A* getInstance ()
        {
            if ( !instance )
                instance = new A ();
            return instance;
        }
};

//implementation file
A* A::instance = NULL;
+12
source

:: ( ., , Java):

A::getInstance();
+4

getInstance A. <class_name>::<static_function_name>.

, .: <class_object>.<static_function_name>

+2

-, ., ::. , , , .

+1

::

.

class::methodName()
+1

All Articles