为什么 - 或者是 - 如下所示强制转换? [英] Why is it -- or is it -- bad to cast like the following code?

查看:86
本文介绍了为什么 - 或者是 - 如下所示强制转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行代码审查,并注意到一些代码写得不好,

我忘记了原因。

这里有一个简短的摘录,名字改为保护无辜,呃,

IP:


class MyPoint

{

..



受保护:

int x;

int y;

}


class MyRect

{

..



MyPoint& MyRect :: TopLeft()

{

返回*((MyPoint *)this);

}


MyPoint& MyRect :: BottomRight()

{

返回*((MyPoint *)this + 1);

}

..



受保护:

LONG left;

LONG top;

LONG权利;

LONG底部;

}

解决方案

ci***********@gmail.com schrieb:


我正在进行代码审查,并发现一些代码写得不好,

我忘记了之所以如此。


这里有一个简短的摘录,名字改为保护无辜,呃,

IP:


class MyPoint

{





受保护:

int x;

int y;

}


class MyRect

{





MyPoint& MyRect :: TopLeft()

{

返回*((MyPoint *)this);

}


MyPoint& MyRect :: BottomRight()

{

返回*((MyPoint *)this + 1);

}





受保护:

LONG left;

LONG top;

LONG right;

LONG bottom;

}



好​​奇优化???


(注意:这与好奇模板不同)


ci *********** @ gmail.com 写道:


我是做一个代码审查,并注意到一些代码写得不好,

我忘记了原因。

这里''一个简短的摘录,名字改为保护无辜,呃,

IP:


级MyPoint

{



>>



受保护:

int x;

int y;

}



;


>

class MyRect

{



>>



MyPoint& MyRect :: TopLeft()

{

返回*((MyPoint *)this);

}


MyPoint& MyRect :: BottomRight()

{

返回*((MyPoint *)this + 1);

}



>>



受保护:

LONG left;



''LONG''未定义。


LONG top;

LONG right;

LONG bottom;

}



;


不好?我不知道。就C ++

而言,此代码具有未定义的行为。不管它是坏还是你决定...


V

-

请删除资金''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问



ci***********@gmail.com 写道:


我正在进行代码审查,并注意到一些代码写得不好,

和我'已经忘记了原因了。


这里有一个简短的摘录,名字改为保护无辜,呃,

IP:


级MyPoint

{





受保护:

int x;

int y;

}


class MyRect

{





MyPoint& MyRect :: TopLeft()

{

返回*((MyPoint *)this);

}


MyPoint& MyRect :: BottomRight()

{

返回*((MyPoint *)this + 1);

}



这些是重新解释的强制转换。取消引用这样的铸造指针是,

我很确定,完全未定义。不仅如此,重新诠释这个+ 1的
演员几乎肯定不是想要的。他们可能认为((MyPoint *)这个)+ 1 $但是这更难以预测。这个

在很多方面都很糟糕。


简而言之,这个类的行为是完全不可预测的。

这个标准没有定义LONG的事实是在

点之后......你不需要定义来看这里的问题。


MyRect应该包含这些函数可以返回的点。

你正在审查的代码是不必要的简洁,其性质是

undefined,即使我们可以在某种程度上猜测目标。






受保护:

LONG left;

LONG top;

LONG right;

LONG bottom;

}


I''m doing a code review and noticed some code that''s not well-written,
and I''ve forgotten the reason why.

Here''s a brief excerpt with names changed to protect the innocent, er,
the IP:

class MyPoint
{
..
:
protected:
int x;
int y;
}

class MyRect
{
..
:
MyPoint & MyRect::TopLeft()
{
return *( ( MyPoint * ) this );
}

MyPoint & MyRect::BottomRight()
{
return *( ( MyPoint * ) this+1 );
}
..
:
protected:
LONG left;
LONG top;
LONG right;
LONG bottom;
}

解决方案

ci***********@gmail.com schrieb:

I''m doing a code review and noticed some code that''s not well-written,
and I''ve forgotten the reason why.

Here''s a brief excerpt with names changed to protect the innocent, er,
the IP:

class MyPoint
{
.
:
protected:
int x;
int y;
}

class MyRect
{
.
:
MyPoint & MyRect::TopLeft()
{
return *( ( MyPoint * ) this );
}

MyPoint & MyRect::BottomRight()
{
return *( ( MyPoint * ) this+1 );
}
.
:
protected:
LONG left;
LONG top;
LONG right;
LONG bottom;
}

Curious Optimization???

(Note: this is different from Curious Templates)


ci***********@gmail.com wrote:

I''m doing a code review and noticed some code that''s not well-written,
and I''ve forgotten the reason why.

Here''s a brief excerpt with names changed to protect the innocent, er,
the IP:

class MyPoint
{
.

>>

protected:
int x;
int y;
}

;

>
class MyRect
{
.

>>

MyPoint & MyRect::TopLeft()
{
return *( ( MyPoint * ) this );
}

MyPoint & MyRect::BottomRight()
{
return *( ( MyPoint * ) this+1 );
}
.

>>

protected:
LONG left;

''LONG'' is undefined.

LONG top;
LONG right;
LONG bottom;
}

;

Bad? I don''t know. This code has undefined behaviour as far as C++
is concerned. Whether it''s bad or not is for you to decide...

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



ci***********@gmail.com wrote:

I''m doing a code review and noticed some code that''s not well-written,
and I''ve forgotten the reason why.

Here''s a brief excerpt with names changed to protect the innocent, er,
the IP:

class MyPoint
{
.
:
protected:
int x;
int y;
}

class MyRect
{
.
:
MyPoint & MyRect::TopLeft()
{
return *( ( MyPoint * ) this );
}

MyPoint & MyRect::BottomRight()
{
return *( ( MyPoint * ) this+1 );
}

These are reinterpret casts. Dereferencing such a casted pointer is,
I''m pretty sure, totally undefined. Not only that but the reinterpret
cast of this+1 is almost certainly not what is wanted. They probably
thought ((MyPoint*)this) + 1 but this is even less predictable. This
is gross in so many ways.

In short, the behavior of this class is totally unpredictable. The
fact that the standard doesn''t define a LONG is quite beside the
point...you don''t need a definition to see the problems here.

MyRect should contain points that can be returned by these functions.
The code you are reviewing is unnecissarily terse and its nature is
undefined even if we can guess at the goal to some degree.

.
:
protected:
LONG left;
LONG top;
LONG right;
LONG bottom;
}


这篇关于为什么 - 或者是 - 如下所示强制转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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