2

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.

6
  • 4
    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. Commented Jan 12, 2017 at 17:13
  • 3
    Fun fact: singleton was tarred and feathered by its authors. Commented Jan 12, 2017 at 17:14
  • Is this the code you compiled when you got the actual error message which you have quoted? Commented Jan 12, 2017 at 17:16
  • Live link: main.cpp:23:23: error: 'static Test* Test::getInstance()' is private within this context Commented Jan 12, 2017 at 17:19
  • What compiler were you using, out of curiosity? I can't think of any modern compilers that would blame print(). Commented Jan 12, 2017 at 17:23

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.