Can a Static method access a private method of the same class?

China☆狼群 提交于 2019-11-29 17:16:29

问题


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?

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.


回答1:


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.




回答2:


According to the standard §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.

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




回答3:


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:

#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;
}



回答4:


Within a function of a class (including static functions), all the private member data and functions are accessible, even if you are dealing with a different instance of that class within that function.

You often exploit this when writing copy constructors and 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.)




回答5:


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.



来源:https://stackoverflow.com/questions/39144979/can-a-static-method-access-a-private-method-of-the-same-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!