静态函数如何访问 class 的私有成员函数(构造函数)
How static function is accessing private member function(constructor) of a class
我遇到了如下代码,它基本上是一个单例 class 的示例,其中我们将 class 构造函数设为私有并提供一个静态 public 函数来创建需要时 class 的实例。
我的问题是当我们在静态函数内部调用new
操作符创建单例对象class时,肯定会调用class的构造函数。我很困惑它是如何发生的,因为据我所知,静态函数只能访问 class 的静态成员和静态函数,那么它如何访问 class 的私有函数(即本例中的构造函数) =27=]?
静态函数可以在不创建任何实例的情况下调用 class 的任何私有函数或 public 成员函数吗?
#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;
}
但是当我编写如下示例代码时:
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;
}
我在使用 g++ 时遇到编译错误:
file.cpp: In static member function ‘static void Sample::statFunc()’:
file.cpp:61: error: cannot call member function ‘void Sample::testFunc()’ without object.
如果我们可以使用静态 public 函数访问 class 的私有函数,那么为什么我会收到此错误?
Can a static function call any private or public member function of class without creating any instance?
您正在创建实例。
instance = new Singleton();
new
关键字创建一个 Singleton
对象。
而且,是的,因为 Singleton::getInstance
是 class 的成员函数,它具有调用构造函数的能力(尽管请注意,您只是间接地这样做),无论它是 static
与否。
回答您稍后添加的问题的第二部分:
class Sample
{
private:
void testFunc()
{
std::cout << "Inside private function" << std::endl;
}
public:
static void statFunc()
{
std::cout << "Inside static function" << std::endl;
Sample s;
s.testFunc(); // this is OK, there is an object (s) and we call
// testFunc upon s
// testFunc(); // this is not OK, there is no object
}
void InstanceFunction()
{
std::cout << "Inside public instance function" << std::endl;
testFunc();
}
};
int main()
{
Sample s;
s.InstanceFunction();
Sample::statFunc();
return 0;
}
无法从 statFunc
内部调用 testFunc();
,因为 testFunc
(私有或非私有)是一个实例函数,您需要一个 Sample
对象 testFunc
可以操作,但是 statFunc
是一个 static
函数,因此没有 Sample
对象。
错误消息对此非常清楚。
只有在提供对象的情况下才能从 statFunc
调用 testFunc
,请参阅上面的代码。
以上代码之所以有效,是因为 getInstance()
的实现调用了不需要对象实例的构造函数。
静态成员函数属于class不属于对象。因此,在调用静态成员函数时没有对象的实例,您无法访问 this
指针,因为没有。如果要从静态函数访问非静态私有成员函数,则需要将对象的引用传递给该函数。例如
例如
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;
};
我遇到了如下代码,它基本上是一个单例 class 的示例,其中我们将 class 构造函数设为私有并提供一个静态 public 函数来创建需要时 class 的实例。
我的问题是当我们在静态函数内部调用new
操作符创建单例对象class时,肯定会调用class的构造函数。我很困惑它是如何发生的,因为据我所知,静态函数只能访问 class 的静态成员和静态函数,那么它如何访问 class 的私有函数(即本例中的构造函数) =27=]?
静态函数可以在不创建任何实例的情况下调用 class 的任何私有函数或 public 成员函数吗?
#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;
}
但是当我编写如下示例代码时:
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;
}
我在使用 g++ 时遇到编译错误:
file.cpp: In static member function ‘static void Sample::statFunc()’:
file.cpp:61: error: cannot call member function ‘void Sample::testFunc()’ without object.
如果我们可以使用静态 public 函数访问 class 的私有函数,那么为什么我会收到此错误?
Can a static function call any private or public member function of class without creating any instance?
您正在创建实例。
instance = new Singleton();
new
关键字创建一个 Singleton
对象。
而且,是的,因为 Singleton::getInstance
是 class 的成员函数,它具有调用构造函数的能力(尽管请注意,您只是间接地这样做),无论它是 static
与否。
回答您稍后添加的问题的第二部分:
class Sample
{
private:
void testFunc()
{
std::cout << "Inside private function" << std::endl;
}
public:
static void statFunc()
{
std::cout << "Inside static function" << std::endl;
Sample s;
s.testFunc(); // this is OK, there is an object (s) and we call
// testFunc upon s
// testFunc(); // this is not OK, there is no object
}
void InstanceFunction()
{
std::cout << "Inside public instance function" << std::endl;
testFunc();
}
};
int main()
{
Sample s;
s.InstanceFunction();
Sample::statFunc();
return 0;
}
无法从 statFunc
内部调用 testFunc();
,因为 testFunc
(私有或非私有)是一个实例函数,您需要一个 Sample
对象 testFunc
可以操作,但是 statFunc
是一个 static
函数,因此没有 Sample
对象。
错误消息对此非常清楚。
只有在提供对象的情况下才能从 statFunc
调用 testFunc
,请参阅上面的代码。
以上代码之所以有效,是因为 getInstance()
的实现调用了不需要对象实例的构造函数。
静态成员函数属于class不属于对象。因此,在调用静态成员函数时没有对象的实例,您无法访问 this
指针,因为没有。如果要从静态函数访问非静态私有成员函数,则需要将对象的引用传递给该函数。例如
例如
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;
};