“*(Vector2*)&x” 在 C++ 中的含义是什么?

huangapple go评论40阅读模式
英文:

What does "*(Vector2*)&x"; in C++ mean?

问题

我正在观看关于C++中联合(unions)的教程,以下是该Youtuber提供的一段代码:

#include <iostream>

struct Vector2
{
     float x, y;
};

struct Vector4
{
     float x, y, z, w;
     Vector2& GetA() 
     {
         return *(Vector2 *)&x;
     }

};

void PrintVector2(const Vector2& vector)
{
    std::cout << vector.x << ", " << vector.y << std::endl;
}

我特别困惑的是这部分代码:

Vector2& GetA() 
{
     return *(Vector2 *)&x;
}

这里到底发生了什么?

这是我尝试理解它的方式:

所以我们获取了x的地址,然后将其分配为Vector2类型的指针,然后对其进行解引用。为什么我们不只返回&x呢?我也困惑于如何将x的地址强制转换为Vector2类型。

英文:

I'm currently watching this tutorial on unions in C++, and here is a snippet of code provided by the Youtuber:

#include &lt;iostream&gt;

struct Vector2
{
     float x, y;
};

struct Vector4
{
     float x, y, z, w;
     Vector2&amp; GetA() 
     {
         return *(Vector2 *)&amp;x;
     }

};

void PrintVector2(const Vector2&amp; vector)
{
    std::cout &lt;&lt; vector.x &lt;&lt; &quot;, &quot;, &lt;&lt; vector.y &lt;&lt; std::endl;
}


I am particularly confused at this part here:


Vector2&amp; GetA() 
    {
         return *(Vector2 *)&amp;x;
     }

What exactly is going on here?

Here is my attempt to try to understand it.

So we are grabbing the address of x, and then assigning it to be a pointer of Vector2 type, and then dereferencing it. Why do we not just, return &amp;x? I'm also confused at how you can type coerce the address of x into a Vector2 type.

答案1

得分: 2

这意味着不多不少,只不过是未定义行为。作者试图告诉编译器“将此Vector4实例视为Vector2实例”,但这是错误的做法。

将其拆分成几个部分:
首先,他们获取了this的第一个成员的地址:

&amp;x

然后,他们进行了C风格的转换,将整个类型系统抛出窗外,告诉编译器“闭嘴,这个指针指向Vector2”:

(Vector2*) &amp;x

最后,他们对指针进行解引用,现在由编译器视为Vector2

* (Vector2*) &amp;x
英文:

It means nothing more, nothing less than Undefined Behaviour. What the author attempted to do is tell compiler "treat this instance of Vector4 as instance of Vector2", but that's plain wrong way to do so.

Breaking it down into pieces:
First they get address of the first member of this

&amp;x 

Then they do a C-style cast, throwing whole type system out of the window to tell compiler "shut up, this pointer points to Vector2"

(Vector2*) &amp;x 

Finally they dereference the pointer, now treated by compiler as Vector2:

* (Vector2*) &amp;x

</details>



# 答案2
**得分**: 2

这段代码告诉编译器假装`x`的地址(即`float*`)实际上是`Vector2`类型对象的地址。当然,实际情况并非如此,编译器没有必要采取任何使结果看起来合理的行动。编写这段代码的人假设`Vector2`中的`float x, y`和`Vector4`中的`float x, y, z, w`之间存在某种神秘的联系,使得`Vector4`成员数据的开头看起来像一个`Vector2`对象。然而,在语言定义中并没有要求这样做。

<details>
<summary>英文:</summary>

The code tells the compiler to pretend that the address of `x` (i.e., `float*`) is actually the address of an object of type `Vector2`. Of course, it&#39;s not, and the compiler is not required to do anything that makes the result seem sensible. Whoever wrote that assumed that the `float x, y` in `Vector2` and the `float x, y, z, w` in `Vector4` have some mystic connection that will make the beginning of that `Vector4` member data look like a `Vector2` object. There is nothing in the language definition that requires this to work.

</details>



huangapple
  • 本文由 发表于 2023年7月13日 22:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定