2
votes

I want to implement class with singleton pattern and have the instance of the class call the private member function.

#include <iostream>
using namespace std;

class Test {
private:
    Test(){}
    static Test* getInstance()
    {
        static Test obj;
        return &obj;
    }
public:
    void print()
    {
        cout<<"test function"<<endl;
    }


};


int main(int argc, const char * argv[]) {
    Test::getInstance()->print(); //error!
    return 0;
}

and I get error message form xcode

'print' is a private member of 'Test'

I think static instance can also call the private member function.

Apologies, I wrote the wrong code here. getInstance() must be public as shown below:

#include <iostream>
using namespace std;

class Test {
private:
    Test(){}
    void print()
    {
        cout<<"test function"<<endl;
    }
public:
    static Test* getInstance()
    {
        static Test obj;
        return &obj;
    }
};

int main(int argc, const char * argv[]) {
    Test::getInstance()->print();
    return 0;
}

The above corrected code is the actual code.

2
I suspect you are using an old compiler. No, The error is from Test::getInstance(), which is a private member function. And hence cannot be accessed outside the class-scope. - WhiZTiM
Fun fact: singleton was tarred and feathered by its authors. - user1593881
Is this the code you compiled when you got the actual error message which you have quoted? - CB Bailey
Live link: main.cpp:23:23: error: 'static Test* Test::getInstance()' is private within this context - qxz
What compiler were you using, out of curiosity? I can't think of any modern compilers that would blame print(). - Justin Time - Reinstate Monica

2 Answers

2
votes

GetInstance is a private function of test, and therefore cannot be accessed from outside the class, you need to make getInstance public.

The fact that the function is static, merely means that an object is not required to access the function, although it can only be accessed from other code that is part of the object.

why have a static private function then? such use can be seen as a part of optimization, as when a function is a member function and not a static one, it implicitly receives the variable 'this' which is not free.

EDIT: because you corrected the code, the correction now should be making the print function public, because a private function can only be called from the class it is a member of, it does not matter if you have an instance of that class, you can, however, call it from inside a static class (you can call it from GetInst, but not from Main).

0
votes

Access control deals with the context you are accessing from. You are accessing a private member of the class. However, the class member access expression (Test::getInstance()->print()) is in the main function. This function has no special permission to access private members. Therefore, means this expression will fail to access object's private members from within the context it appears.