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

ufooler 2008-11-19 01:20:45
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的函数中为什么能够访问一个对象的私有变量(函数也可以)。


各位大侠帮忙。。。说道说道。
...全文
891 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
iBug168 2008-11-21
  • 打赏
  • 举报
回复
学习了..
hityct1 2008-11-20
  • 打赏
  • 举报
回复
7楼的没看懂。19楼所答非所问。
int test::data(test *_this)
{
return _this->__m_h_data;
}
tunnel115 2008-11-20
  • 打赏
  • 举报
回复
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;
}

可以看成是个全局函数(不过,和全局函数有区别)
Kinglin_sky 2008-11-20
  • 打赏
  • 举报
回复
楼上高见!
lann64 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 hityct1 的回复:]
7楼的没看懂。19楼所答非所问。
int test::data(test *_this)
{
return _this->__m_h_data;
}
[/Quote]
呵呵,怎么没看懂?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楼说的。没有答非所问,也没有看不懂的。呵呵~~~~加油。

cattycat 2008-11-19
  • 打赏
  • 举报
回复
7楼说的对,你这里实际访问的是 *_this的成员变量,如果你直接
return __m_h_data;
就会出错,static函数访问类的static成员变量。
星羽 2008-11-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
bbb332 2008-11-19
  • 打赏
  • 举报
回复
[Quote=引用 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;
}

可以访问别的同类对象的的变量
--…
[/Quote]...
kitsudo 2008-11-19
  • 打赏
  • 举报
回复
[Quote=引用 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;
}

可以访问别的同类对象的的变量
--------------------------
这…
[/Quote]
解释一下
当编译器解释_this->__m_h_data; 时
首先知道_this是一个test类
然后他确定了自己(data函数)是test类(不违反访问private成员__m_h_data的契约)
所以对于_this来说他允许从_this这个实例对象中找到__m_h_data这个成员
并进行赋值或取值操作
lunarfan 2008-11-19
  • 打赏
  • 举报
回复
[Quote=引用 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;
}
[/Quote]
可以访问别的同类对象的的变量
--------------------------
这个变量要求是static的,还是什么都行?
wqzh_wzhy 2008-11-19
  • 打赏
  • 举报
回复
up 7楼的分析!!!
lgccaa 2008-11-19
  • 打赏
  • 举报
回复
mark
toadzw 2008-11-19
  • 打赏
  • 举报
回复
static函数也是类的成员函数啊,当然也是可以的,只是这个函数为所有的对象所有罢了,实际上static函数很少用到,用到时一般就是用来访问static成员变量的啊;
kitsudo 2008-11-19
  • 打赏
  • 举报
回复
这点我感觉应该没有冲突的

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

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

作者是: KenArnold JamesGosling DavidHolmes
herman~~ 2008-11-19
  • 打赏
  • 举报
回复
7楼的说法正解

static是不可以访问自身非static成员的!
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量
tnfyj 2008-11-19
  • 打赏
  • 举报
回复

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;
}
太乙 2008-11-19
  • 打赏
  • 举报
回复
7L
对!
ghostxyz0 2008-11-19
  • 打赏
  • 举报
回复
[Quote=引用 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;
}
[/Quote]

UP
就呆在云上 2008-11-19
  • 打赏
  • 举报
回复
此_this非彼this ,_this是其他的对象,this是自身
static是不可以访问自身非static成员的!
你这里可以访问是因为,data是一个成员函数,可以访问别的同类对象的的变量
但是不可以访问自身的:

int test::data(test *_this)
{
//__m_h_data = 10;就不对了
return _this->__m_h_data;
}
Non_Recursive 2008-11-19
  • 打赏
  • 举报
回复
up..另一帖回了^_^
加载更多回复(5)

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧