C++ 静态函数如何访问类的私有成员函数(构造函数)
我遇到了下面这样的代码,这基本上是一个单例类的示例,在这个示例中,我们将类构造函数设置为私有,并在需要时提供一个静态公共函数来创建类的实例 我的问题是,当我们调用C++ 静态函数如何访问类的私有成员函数(构造函数),c++,singleton,private-members,static-functions,C++,Singleton,Private Members,Static Functions,我遇到了下面这样的代码,这基本上是一个单例类的示例,在这个示例中,我们将类构造函数设置为私有,并在需要时提供一个静态公共函数来创建类的实例 我的问题是,当我们调用new操作符在静态函数中创建singleton类的对象时,肯定会调用该类的构造函数。我对它是如何发生的感到困惑,因为据我所知,静态函数只能访问类的静态成员和静态函数。那么它如何访问类的私有函数(构造函数) 静态函数能否在不创建任何实例的情况下调用类的任何私有或公共成员函数 #include <iostream> using
new
操作符在静态函数中创建singleton类的对象时,肯定会调用该类的构造函数。我对它是如何发生的感到困惑,因为据我所知,静态函数只能访问类的静态成员和静态函数。那么它如何访问类的私有函数(构造函数)
静态函数能否在不创建任何实例的情况下调用类的任何私有或公共成员函数
#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;
}
若我们可以使用静态公共函数访问类的私有函数,那个么为什么会出现这个错误呢
静态函数能否在不创建任何实例的情况下调用类的任何私有或公共成员函数
#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;
}
您正在创建一个实例
instance = new Singleton();
new
关键字创建一个Singleton
对象
而且,是的,因为
Singleton::getInstance
是类的成员函数,所以它能够调用构造函数(注意,您只是间接地这样做),无论它是静态的还是非静态的。回答您稍后添加的问题的第二部分:
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;
}
类示例
{
私人:
void testFunc()
{
std::cout上述代码工作的原因是getInstance()
的实现调用构造函数,而构造函数不需要对象的实例
静态成员函数属于类而不是对象。因此,在调用静态成员函数时,没有对象的实例,因此无法访问this
指针,因为没有指针。如果要从静态函数访问非静态私有成员函数,则需要将对象的引用传递给功能
e、 g
class-foo{
公众:
foo(int i):myInt(i){}
静态int myStaticMethod(foo&obj);
私人:
int-myInt;
};
intfoo::myStaticMethod(foo&obj){
返回obj.myInt;
}
#包括
int main(){
foo f(1),;
std::cout简短回答:IMHO是的。而且IMHO你的代码没有问题。getInstance
是Singleton
类的成员,因此它可以调用该类的所有方法,甚至是私有方法。我认为,与此类似,Singleton类的构造函数是一个类函数,不能被静态函数调用如果构造函数可以在静态函数中被调用,那么为什么不使用普通的私有函数呢?@Vikram记住,构造函数不是普通的成员函数。当你编写instance=new Singleton()时;
您正在创建一个对象,构造函数被调用到正在创建的对象上。@Vikram,我已经编辑了答案,现在有一个示例,您可以从statFunc
中调用testFunc
。感谢您的回答。因此,我从上面了解到,as-ctor是一个特殊的成员函数,它没有需要调用任何对象,这样就可以使用静态函数(没有任何对象)调用它,但是类的其他普通成员函数需要类实例来调用它们,这样就不能在没有对象的静态函数中调用它们。是的,非常多
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;
}
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;
};