I came across code like the below, which is basically an example of a singleton class in which we make the class constructor private and provide one static public function to create an instance of the class when required.
My question is when we call the new
operator to create an object of the singleton class inside the static function, then the constructor of the class will surely be called. I am confused how it happens because, as far I know, a static function can only access static members and static functions of a class then how can it access a private function (i.e. the constructor in this case) of a class?
Can a static function call any private or public member function of class without creating any instance?
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *getInstance();
private:
Singleton(){}
static Singleton* instance;
};
Singleton* Singleton::instance = 0;
Singleton* Singleton::getInstance()
{
if(!instance) {
instance = new Singleton(); //private ctor will be called
cout << "getInstance(): First instance\n";
return instance;
}
else {
cout << "getInstance(): previous instance\n";
return instance;
}
}
int main()
{
Singleton *s1 = Singleton::getInstance();
Singleton *s2 = Singleton::getInstance();
return 0;
}
But when I wrote a sample code as below:
class Sample
{
private:
void testFunc()
{
std::cout << "Inside private function" <<std::endl;
}
public:
static void statFunc()
{
std::cout << "Inside static function" <<std::endl;
testFunc();
}
};
int main()
{
Sample::statFunc();
return 0;
}
I get a compilation error with g++ :
file.cpp: In static member function ‘static void Sample::statFunc()’:
file.cpp:61: error: cannot call member function ‘void Sample::testFunc()’ without object.
If we can access private function of class with static public function then why i am getting this error?
The reason the above code works is because the implementation of getInstance()
calls the constructor which does not requrire an instance of the object.
Static member functions belong to the class not the object. Hence there is no instance of an object when calling a static member function, you cannot access the this
pointer becuase there isn't one. If you want to access non-static private member functions from a static function the reference of the object needs to be passed to the function. e.g.
e.g.
class foo {
public:
foo(int i) : myInt(i) {}
static int myStaticMethod(foo & obj);
private:
int myInt;
};
int foo::myStaticMethod(foo & obj) {
return obj.myInt;
}
#include <iostream>
int main() {
foo f(1);
std::cout << foo::myStaticMethod(f);
return 0;
};