A *到A& ? [英] casting A* to A& ?

查看:63
本文介绍了A *到A& ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下操作时会发生什么:


struct A {

int i;

string j;

A(){}

};


void f(A& a){

cout<< a.i<<结束;

}


int _tmain(int argc,_TCHAR * argv [])

{

A a;

ai = 6;

A * pa =& a;

f((A&)pa); //关键点

f(* pa); //按预期工作

返回0;

}

为什么这是合法的呢?

最近我发现有些人认为(A&)pa相当于* pa,而

不是根据上面的例子。 (f((A&)pa)的输出看起来像

一些整数,但如果它意味着f(* pa),肯定不是6。)

What happens exactly when I do the following:

struct A {
int i;
string j;
A() {}
};

void f(A& a) {
cout << a.i << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
A a;
a.i = 6;
A* pa = &a;
f((A&)pa); // CRITICAL POINT
f(*pa); // works as expected
return 0;
}
How come that this is legal, anyway?
Lately I''ve seen some people expect that (A&)pa be equivalent to *pa, which
is not according to the example above. (The output for f((A&)pa) looks like
some integer, but surely not 6 as should be if it meant f(*pa).)

推荐答案

Agoston Bejo写道:
Agoston Bejo wrote:
当我执行以下操作时会发生什么:

struct A {
int i;
字符串j;
A(){}
};

void f(A& a){
cout<< a.i<< endl;
}
int _tmain(int argc,_TCHAR * argv [])
{
a a;
ai = 6;
A * pa =& a;
f((A&)pa); //关键点
f(* pa); //按预期工作
返回0;
}

为什么这是合法的呢?


您可以将地址转换为同一地址的其他内容。

尝试使用常量执行相同的转换,您会看到那不是

允许的。


最近我看到有人认为(A&)pa相当于* pa,
这不是根据上面的例子。


不,不是。 * pa是对pa指向的任何内容的引用,tho。所以

f(* pa);

非常好。



f的输出((A& amp; ;)pa)看起来像一些整数,但肯定不是6应该如果
它意味着f(* pa)。)
What happens exactly when I do the following:

struct A {
int i;
string j;
A() {}
};

void f(A& a) {
cout << a.i << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
A a;
a.i = 6;
A* pa = &a;
f((A&)pa); // CRITICAL POINT
f(*pa); // works as expected
return 0;
}
How come that this is legal, anyway?
You can cast something at an address to something else at the same address.
Try doing the same cast with a constant and you''ll see that that is not
allowed.

Lately I''ve seen some people expect that (A&)pa be equivalent to *pa,
which is not according to the example above.
No, it is not. *pa is a reference to whatever pa is pointing to, tho. So
f(*pa);
is perfectly fine.

(The output for
f((A&)pa) looks like some integer, but surely not 6 as should be if
it meant f(*pa).)




整数相等实际发生的事情是你告诉编译器看这里,pa是

而不是指针''到达地址。 'A型结构'。这实际上是错误的,但是因为

你告诉编译器你知道的更好,所以你就是这样做的。并且

你很幸运,因为pa的大小恰好与A实例的大小相同。碰巧的是,当你进行

演员时,p的值重叠了A :: i。


因此,你只会在这个简单的例子中得到伪造的结果,实际上你会通常会获得访问冲突或其他一些hardtofind错误。

换句话说,当你使用类型转换时,你告诉了编译器

你知道你在做什么。如果你不知道你在做什么,告诉

编译器是一件非常愚蠢的事情......

(是的,引用已经实现和指针一样。但它们有不同的语义。)

-

Sigurd
http://utvikling.com


感谢您的全面解答!

" Sigurd Stenersen" < SI ***** @ utvikling.com>在留言中写道

新闻:%2 ****************** @ TK2MSFTNGP15.phx.gbl ...


[...]
Thanks for the thorough answer!
"Sigurd Stenersen" <si*****@utvikling.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...

[...]
实际发生的事情是你告诉编译器看这里,pa
不是它的指针一个类型A的结构。这实际上是错误的,但是
,因为你告诉编译器你知道的更好,它让你做到了。并且


那么(Type)var是否与reinterpret_cast< Type>(var)相同?

我认为这意味着static_cast< Type>(var) 。

或者它是否依赖于上下文,如何解释C风格的演员表?

你很幸运,因为pa的大小恰好与
一个实例的大小。实际上,当你执行
时,p的值与A :: i重叠。
What actually happens is you''re telling the compiler that "look here, pa is not a pointer it''s a struct of type A". That is in fact wrong, but because you''re telling the compiler that you know better it let''s you do it. And
So does (Type)var mean the same as reinterpret_cast<Type>(var)?
I thought it meant static_cast<Type>(var).
Or is it context-dependent, how C-style casts are interpreted?
you''re lucky, because the size of pa happens to be the same as the size of
an A instance. As it happens, the value of p overlaps A::i when you do the cast.




我认为第一个语句(关于大小)是并不是真的相关,重要的是,
的重要性在于A实例的大小至少与

pa的大小一样大,所以没有堆外存储器是读取可能导致访问冲突的内容。我好吗?b $ b我是对吗?


然而,我已经尝试了A只有一个char成员而没有别的,

但是在这种情况下也没有发生访问冲突。 (我在调试和发布模式下编译了

示例。)

我认为它应该产生一些运行时错误,特别是在调试中

模式,不应该吗?




I think the first statement (about the sizes) is not really relevant, what
matters is that the size of an A instance is at least as big as the size of
pa, so no out-of-heap memory is read which could cause access violation. Am
I right?

However, I''ve tried it with A having only a char member and nothing else,
but no access violation occured in this case, either. (I compiled the
example both in debug and release mode.)
I think it should have produced some runtime error, especially in debug
mode, shouldn''t it?



您好,
当我发生时会发生什么执行以下操作:

struct A {
int i;
string j;
A(){}
};

void f(A& a){
cout<< a.i<< endl;
}
int _tmain(int argc,_TCHAR * argv [])
{
a a;
ai = 6;
A * pa =& a;
f((A&)pa); //关键点
f(* pa); //按预期工作
返回0;
}

为什么这是合法的呢?
最近我看到有些人期待(A& amp; ;)pa相当于* pa,
,这不是上面的例子。 (f((A&)pa)的输出看起来像某个整数一样是
,但如果它意味着f(* pa),肯定不是6。)
What happens exactly when I do the following:

struct A {
int i;
string j;
A() {}
};

void f(A& a) {
cout << a.i << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
A a;
a.i = 6;
A* pa = &a;
f((A&)pa); // CRITICAL POINT
f(*pa); // works as expected
return 0;
}
How come that this is legal, anyway?
Lately I''ve seen some people expect that (A&)pa be equivalent to *pa, which is not according to the example above. (The output for f((A&)pa) looks like some integer, but surely not 6 as should be if it meant f(*pa).)




如果你使用C ++样式演员表示法。

唯一有效的代码是:f(reinterpret_cast< A&>(pa)); // A *& - > A&。

当您在代码中看到* _cast运算符时,这通常意味着程序可以更好地编写
。但是当你看到reinterpret_cast时,这通常意味着什么是错误的,除非是低级代码。

-

弗拉基米尔Nesterovsky

电子邮件: vl ****** @ nesterovsky-bros.com

home: http://www.nesterovsky-bros。 com


这篇关于A *到A&amp; ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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