gpt4 book ai didi

c++ - 'this' 用于结构?

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:46 25 4
gpt4 key购买 nike

一个简短的,也许是愚蠢的问题。对于类和结构,有时我喜欢使用与构造函数参数同名的成员变量。例如:

class Vector3
{
float x, y, z;

public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}

基本上,我也想对结构执行此操作,但您不能在结构构造函数中使用“this”(我认为它们的使用是为类保留的)。有什么办法可以做到这一点,还是我应该为我的论点起个别的名字?

最佳答案

您可以使用它,但它是一个指针,就像在类中一样:

this->x = x;

顺便说一下,structclass 除了默认的access specifiers 外,其他的东西完全一样。

关于c++ - 'this' 用于结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com