C++ 类中的static函数,访问私有变量。

时间:2020-12-19 16:39:10
class test
{
public:
   test();
   ~test();

   static  int data  (test *_this);

private:

   int             __m_h_data;

};

int test::data(test *_this)
{
   return _this->__m_h_data;
}


在class中的static的函数中为什么能够访问一个对象的私有变量(函数也可以)。


各位大侠帮忙。。。说道说道。

25 个解决方案

#1


up

#2


就算不是static也可以访问啊...私有的意思是对外私有...

#3


static的函数 好像能访问static 变量

#5


自己当然可以用自己的人

#6


up..另一帖回了^_^

#7


此_this非彼this ,_this是其他的对象,this是自身
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 

int test::data(test *_this)
{
    //__m_h_data = 10;就不对了
    return _this->__m_h_data;
}

#8


引用 7 楼 ztz0223 的回复:
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ code
int test::data(test *_this)
{
    //__m_h_data = 10;就不对了
    return _this->__m_h_data;
}


UP

#9


7L
对!

#10



class test
{
public:
   test();
   ~test();

   static  int data  (test *_this);

private:

   int             __m_h_data;

};

int test::data(test *_this)//这里定义的时候貌似少了static修饰符呃... 这样的程序不会有连接错误么?
{
   return _this->__m_h_data;
}

#11


7楼的说法正解

static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 

#12


这点我感觉应该没有冲突的

这里的理解应该明确为
对于编译器来说
一个类里面的代码是可以有权限访问该 的私有成员
类中的私有成员所承载的契约是有此类的开发者维护的
所以放开是这点允许访问并不会导致破坏契约的情况放生

<Java程序设计语言>中阐述protected的真正一节
所讲述的应该和这个是一个意思
楼主可以看看

作者是: KenArnold JamesGosling DavidHolmes

#13


static函数也是类的成员函数啊,当然也是可以的,只是这个函数为所有的对象所有罢了,实际上static函数很少用到,用到时一般就是用来访问static成员变量的啊;

#14


mark

#15


up 7楼的分析!!!

#16


引用 7 楼 ztz0223 的回复:
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ codeint test::data(test *_this)
{
    //__m_h_data = 10;就不对了
    return _this->__m_h_data;
}

可以访问别的同类对象的的变量
--------------------------
这个变量要求是static的,还是什么都行?

#17


引用 16 楼 lunarfan 的回复:
引用 7 楼 ztz0223 的回复:
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ codeint test::data(test *_this) 

    //__m_h_data = 10;就不对了 
    return _this->__m_h_data; 

 
可以访问别的同类对象的的变量 
-------------------------- 
这…

解释一下
当编译器解释_this->__m_h_data; 时
首先知道_this是一个test类
然后他确定了自己( data函数)是test类(不违反访问private成员__m_h_data的契约)
所以对于_this来说他 允许从_this这个实例对象中 找到__m_h_data这个成员
并进行 赋值或取值操作

#18


引用 17 楼 kitsudo 的回复:
引用 16 楼 lunarfan 的回复:
引用 7 楼 ztz0223 的回复: 
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ codeint test::data(test *_this) 

    //__m_h_data = 10;就不对了 
    return _this->__m_h_data; 


可以访问别的同类对象的的变量 
--…
...

#19


iso c++ 标准里 11 Member access control

1 A member of a class can be
— private; that is, its name can be used only by members and friends of the class in which it is
declared.
— protected; that is, its name can be used only by members and friends of the class in which it is
declared, and by members and friends of classes derived from this class (see 11.5).
— public; that is, its name can be used anywhere without access restriction.


这里是标准对Member functions的定义

Member functions [class.mfct]
1 Functions declared in the definition of a class, excluding those declared with a friend specifier (11.4),
are called member functions of that class. A member function may be declared static in which case it is
a static member function of its class (9.4); otherwise it is a nonstatic member function of its class (9.3.1,
9.3.2).

所以 Member functions 包括static Member functions 和nostatic Member functions 

#20


7楼说的对,你这里实际访问的是 *_this的成员变量,如果你直接 
return  __m_h_data;
就会出错,static函数访问类的static成员变量。

#21


static是类的组成部分,但是不是任何对象的组成部分
class test
{
public:
   test();
   ~test();

   static  int data  (test *_this);

private:

   int             __m_h_data;

};

int test::data(test *_this)
{
   return _this->__m_h_data;
}

可以看成是个全局函数(不过,和全局函数有区别)

#22


7楼的没看懂。19楼所答非所问。
int test::data(test *_this)
{
   return _this->__m_h_data;
}

#23


引用 22 楼 hityct1 的回复:
7楼的没看懂。19楼所答非所问。 
int test::data(test *_this) 

  return _this->__m_h_data; 

呵呵,怎么没看懂?7楼说得是核心,19楼说得是关键。

这里其实说了两件事。1、类内的静态函数也是类的成员,也一样遵守类内成员的访问权限。从访问权限上说,静态函数data是类test的成员,就有权访问私有变量。
那么为什么静态函数又不能直接访问呢,比如直接访问 __m_h_data变量是通不过编译的。这是因为静态函数实际上是个全局的,不属于任何一个类对象,也就是说静态函数没有绑定this指针,这样一来,静态函数只是在类型定义上是属于这个类的,但它不知道类的每个对象在哪,也就无法直接访问类内的成员了。
那么楼主的问题又是什么原因呢?这就是第2间事了。
2、其实楼主是被_this这个名字迷惑了,这个_this只是个形参,跟绑定类内函数身上那个this不是一回事。这里要是把_this换个别的名字就好理解多了。比如:

int test::data(test *_that) 

  return _that->__m_h_data; 


这里1就是19楼说的,2就是7楼说的。没有答非所问,也没有看不懂的。呵呵~~~~加油。

#24


楼上高见!

#25


学习了..

#1


up

#2


就算不是static也可以访问啊...私有的意思是对外私有...

#3


static的函数 好像能访问static 变量

#4


#5


自己当然可以用自己的人

#6


up..另一帖回了^_^

#7


此_this非彼this ,_this是其他的对象,this是自身
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 

int test::data(test *_this)
{
    //__m_h_data = 10;就不对了
    return _this->__m_h_data;
}

#8


引用 7 楼 ztz0223 的回复:
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ code
int test::data(test *_this)
{
    //__m_h_data = 10;就不对了
    return _this->__m_h_data;
}


UP

#9


7L
对!

#10



class test
{
public:
   test();
   ~test();

   static  int data  (test *_this);

private:

   int             __m_h_data;

};

int test::data(test *_this)//这里定义的时候貌似少了static修饰符呃... 这样的程序不会有连接错误么?
{
   return _this->__m_h_data;
}

#11


7楼的说法正解

static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 

#12


这点我感觉应该没有冲突的

这里的理解应该明确为
对于编译器来说
一个类里面的代码是可以有权限访问该 的私有成员
类中的私有成员所承载的契约是有此类的开发者维护的
所以放开是这点允许访问并不会导致破坏契约的情况放生

<Java程序设计语言>中阐述protected的真正一节
所讲述的应该和这个是一个意思
楼主可以看看

作者是: KenArnold JamesGosling DavidHolmes

#13


static函数也是类的成员函数啊,当然也是可以的,只是这个函数为所有的对象所有罢了,实际上static函数很少用到,用到时一般就是用来访问static成员变量的啊;

#14


mark

#15


up 7楼的分析!!!

#16


引用 7 楼 ztz0223 的回复:
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ codeint test::data(test *_this)
{
    //__m_h_data = 10;就不对了
    return _this->__m_h_data;
}

可以访问别的同类对象的的变量
--------------------------
这个变量要求是static的,还是什么都行?

#17


引用 16 楼 lunarfan 的回复:
引用 7 楼 ztz0223 的回复:
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ codeint test::data(test *_this) 

    //__m_h_data = 10;就不对了 
    return _this->__m_h_data; 

 
可以访问别的同类对象的的变量 
-------------------------- 
这…

解释一下
当编译器解释_this->__m_h_data; 时
首先知道_this是一个test类
然后他确定了自己( data函数)是test类(不违反访问private成员__m_h_data的契约)
所以对于_this来说他 允许从_this这个实例对象中 找到__m_h_data这个成员
并进行 赋值或取值操作

#18


引用 17 楼 kitsudo 的回复:
引用 16 楼 lunarfan 的回复:
引用 7 楼 ztz0223 的回复: 
此_this非彼this ,_this是其他的对象,this是自身 
static是不可以访问自身非static成员的! 
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量 
但是不可以访问自身的: 


C/C++ codeint test::data(test *_this) 

    //__m_h_data = 10;就不对了 
    return _this->__m_h_data; 


可以访问别的同类对象的的变量 
--…
...

#19


iso c++ 标准里 11 Member access control

1 A member of a class can be
— private; that is, its name can be used only by members and friends of the class in which it is
declared.
— protected; that is, its name can be used only by members and friends of the class in which it is
declared, and by members and friends of classes derived from this class (see 11.5).
— public; that is, its name can be used anywhere without access restriction.


这里是标准对Member functions的定义

Member functions [class.mfct]
1 Functions declared in the definition of a class, excluding those declared with a friend specifier (11.4),
are called member functions of that class. A member function may be declared static in which case it is
a static member function of its class (9.4); otherwise it is a nonstatic member function of its class (9.3.1,
9.3.2).

所以 Member functions 包括static Member functions 和nostatic Member functions 

#20


7楼说的对,你这里实际访问的是 *_this的成员变量,如果你直接 
return  __m_h_data;
就会出错,static函数访问类的static成员变量。

#21


static是类的组成部分,但是不是任何对象的组成部分
class test
{
public:
   test();
   ~test();

   static  int data  (test *_this);

private:

   int             __m_h_data;

};

int test::data(test *_this)
{
   return _this->__m_h_data;
}

可以看成是个全局函数(不过,和全局函数有区别)

#22


7楼的没看懂。19楼所答非所问。
int test::data(test *_this)
{
   return _this->__m_h_data;
}

#23


引用 22 楼 hityct1 的回复:
7楼的没看懂。19楼所答非所问。 
int test::data(test *_this) 

  return _this->__m_h_data; 

呵呵,怎么没看懂?7楼说得是核心,19楼说得是关键。

这里其实说了两件事。1、类内的静态函数也是类的成员,也一样遵守类内成员的访问权限。从访问权限上说,静态函数data是类test的成员,就有权访问私有变量。
那么为什么静态函数又不能直接访问呢,比如直接访问 __m_h_data变量是通不过编译的。这是因为静态函数实际上是个全局的,不属于任何一个类对象,也就是说静态函数没有绑定this指针,这样一来,静态函数只是在类型定义上是属于这个类的,但它不知道类的每个对象在哪,也就无法直接访问类内的成员了。
那么楼主的问题又是什么原因呢?这就是第2间事了。
2、其实楼主是被_this这个名字迷惑了,这个_this只是个形参,跟绑定类内函数身上那个this不是一回事。这里要是把_this换个别的名字就好理解多了。比如:

int test::data(test *_that) 

  return _that->__m_h_data; 


这里1就是19楼说的,2就是7楼说的。没有答非所问,也没有看不懂的。呵呵~~~~加油。

#24


楼上高见!

#25


学习了..

相关文章