通过类型转换访问类的私有成员 [英] Accessing private member of a class through type-casting

查看:76
本文介绍了通过类型转换访问类的私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码


A级{

私人:

int a;

};


int main(无效){

A x;

int * ptr =(int *)& x ;

* x = 10;

返回0;

}


我明白我不能改变类的私有属性,

但标准对上面的代码有什么看法?

Consider the code

class A {
private:
int a;
};

int main(void) {
A x;
int* ptr = (int*)&x;
*x = 10;
return 0;
}

I understand that I can not change the private attributes of a class,
but what does standard have to say about the code above?

推荐答案



dragoncoder写道:

dragoncoder wrote:
考虑代码

A类{
私人:
int a ;
};

int main(void){
A x;
int * ptr =(int *)& x;
* x = 10;
返回0;
}

我明白我不能改变类的私有属性,
但是标准对于上面的代码?
Consider the code

class A {
private:
int a;
};

int main(void) {
A x;
int* ptr = (int*)&x;
*x = 10;
return 0;
}

I understand that I can not change the private attributes of a class,
but what does standard have to say about the code above?




它的行为未定义。它可能会工作......它可能不会......它

可能会结束你所知道的世界......未定义。



That its behavior is undefined. It might work...it might not...it
might end the world as you know it...undefined.


dragoncoder发布:
dragoncoder posted:
考虑代码

A类{
私人:
int a;
} ;

int main(无效){
A x;


您已经创建了一个类型为A的对象。该对象包含在其中的
类型为int的对象。


int * ptr =(int *)& x;


表达式& x是A *类型的(省略const)。你有一个旧的C风格的演员阵容将它变成一个int *。

。这是非法的。


即使您的代码是合法的,您也可以假设

" x"等于x.a的地址。我不确定标准是否给予

任何此类保证。如果对象是包含虚拟

方法的类,那么你的代码将更不可靠。

* x = 10;


x的值是未定义的,所以你的上述

语句的行为是未定义的。

返回0;
}
Consider the code

class A {
private:
int a;
};

int main(void) {
A x;
You''ve create an object of the type "A". This object contains within it
an object of the type "int".

int* ptr = (int*)&x;
The expression "&x" is of the type "A*" (leaving out const). You have
used an old C-style cast to turn it into an "int*". This is illegal.

Even if your code was legal, you make the assumption that the address of
"x" is equal to the address of "x.a". I''m not sure if the Standard gives
any such guarantee. If the object was of a class which contained virtual
methods, your code would be even less reliable.
*x = 10;
The value of "x" is undefined, so your the behaviour of the above
statement is undefined.
return 0;
}



-Tomás


-Tomás




dragoncoder写道:

dragoncoder wrote:
int main(void){
A x;
int * ptr =(int *)& x;
* x = 10;
int main(void) {
A x;
int* ptr = (int*)&x;
*x = 10;




哦,我在这里错过了什么。 * ptr = 10;本来是未定义的。 * x

= 10无法编译,因为A没有操作员*。


BTW,(int *)& x是完全合法。它是一个C风格的演员阵容,并且与所有C风格的演员阵容一样,它可以做出意想不到的事情。这个

解析的是reinterpret_cast< int *>(& x)。这当然是合法的

但是结果不明确。 static_cast是非法的并且使用

C ++转换机制会警告你关于演员的未定义的

性质......这就是为什么C-Style演员阵容很糟糕。



Oh, I missed something here. *ptr = 10; would have been undefined. *x
= 10 doesn''t compile as A doesn''t have an operator*.

BTW, (int*)&x is totally legal. It is a C-style cast and as is the
case with all C-style casts it can do things unexpectedly. What this
resolves to is a reinterpret_cast<int*>(&x). This is of course legal
but has undefined results. A static_cast would be illegal and using
the C++ casting mechanism would have warned you about the undefined
nature of the cast...this is why C-Style casts are bad.


这篇关于通过类型转换访问类的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆