C++ 静态方法可以访问同一个类的私有方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39144979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a Static method access a private method of the same class?
提问by Flowing Cloud
I have this question because of the singleton/named constructor. In both cases, the real constructors are protected or private, neither of which can be accessed from outside.
由于单例/命名构造函数,我有这个问题。在这两种情况下,真正的构造函数都是受保护的或私有的,两者都不能从外部访问。
For example, a short named constructor is this:
例如,一个简短的命名构造函数是这样的:
class A
{
public:
static A createA() { return A(0); } // named constructor
private:
A (int x);
};
int main(void)
{
A a = A::createA();
}
I thought static method can only access static data member, or access private data/method via an existing object.
However, in the above code, private constructor A()
isn't static, and at the time it is being called, no object exists either.
So the only explanation I can think of is that static method can access non-static private method of the same class. Can anyone please either affirm or negate my thought, possibly with some lines of explanations?
我认为静态方法只能访问静态数据成员,或通过现有对象访问私有数据/方法。但是,在上面的代码中,私有构造函数A()
不是静态的,并且在调用它时,也不存在任何对象。所以我能想到的唯一解释就是静态方法可以访问同一个类的非静态私有方法。任何人都可以肯定或否定我的想法,可能有一些解释吗?
I apologize if this is too trivial however the key words are too common and I wasn't able to find an answer in dozens of google pages. Thanks in advance.
如果这太琐碎,我深表歉意,但是关键词太常见了,我无法在数十个谷歌页面中找到答案。提前致谢。
回答by NathanOliver
A static member function has the same access rights as a non static member function. So yes, it can access any public, protected, and private variable in the class. However you need to pass an instance of the class to the function for the function to be able to access the member. Otherwise a static function can only directly access any other static member in the class.
静态成员函数与非静态成员函数具有相同的访问权限。所以是的,它可以访问类中的任何公共、受保护和私有变量。但是,您需要将类的实例传递给函数,以便函数能够访问该成员。否则,静态函数只能直接访问类中的任何其他静态成员。
回答by 101010
According to the standard §11/p2 Member access control [class.access] (Emphasis Mine):
根据标准§11/p2 Member access control [class.access] (Emphasis Mine):
A member of a class can also access all the names to which the class has access.A local class of a member function may access the same names that the member function itself may access.113
113) Access permissions are thus transitive and cumulative to nested and local classes.
类的成员还可以访问该类有权访问的所有名称。成员函数的局部类可以访问与成员函数本身可以访问的名称相同的名称。113
113) 因此,访问权限对于嵌套类和本地类是可传递和累积的。
Since a static member function is a member of a class it has access to all the names to which the class has access and consequently to the constructor of the class itself.
由于静态成员函数是类的成员,因此它可以访问该类可以访问的所有名称,因此可以访问该类本身的构造函数。
Consequently, in your example:
因此,在您的示例中:
class A {
A(int x);
public:
static A createA() { return A(0); } // named constructor
};
static member function A::createA()
has access to call private
constructor A::A(int)
.
静态成员函数A::createA()
可以调用private
构造函数A::A(int)
。
回答by Fitzwilliam Bennet-Darcy
Within a function of a class (including static
functions), allthe private
member data and functions are accessible, even if you are dealing with a different instanceof that class within that function.
在类(包括函数static
功能),所有的private
成员数据和函数都可以访问,即使你是在处理不同的情况下该函数内的类。
You often exploit this when writing copy constructorsand assignment operators.
您在编写复制构造函数和赋值运算符时经常利用这一点。
(My boss often talks about how he would like to be able to disable this behaviour using some kind of friend = delete;
syntax.)
(我的老板经常谈论他希望如何使用某种friend = delete;
语法来禁用这种行为。)
回答by rainer
Yes, it can. The static function can access private members, but other than that it is just like any function defined outside of the class. Especially, since it doesn't have a this
pointer (ie. is not "bound" to any specific instance), you won't be able to access any members directly (which are always "bound" to an instance): if you wanted to do that, you need a an instance from somewhere:
是的,它可以。静态函数可以访问私有成员,但除此之外,它就像在类之外定义的任何函数一样。特别是,由于它没有this
指针(即未“绑定”到任何特定实例),您将无法直接访问任何成员(始终“绑定”到实例):如果您愿意为此,您需要一个来自某处的实例:
#include <iostream>
using namespace std;
class A
{
public:
static A createA() { return A(0); }
static void dosomething(A *a) { return a->something(); }
private:
A (int x) { cout << "ctor" << endl; }
void something() { cout << "something" << endl; }
};
int main(void)
{
A a = A::createA();
A::dosomething(&a);
return 0;
}
回答by Robert Kock
Your static method is not accessing any static member nor any non-static member of an existing instance.
It's just creating a new instance.
您的静态方法未访问现有实例的任何静态成员或任何非静态成员。
它只是创建一个新实例。