我得到了这个我不太明白的语法:
class USphereComponent* ProxSphere;
Run Code Online (Sandbox Code Playgroud)
我认为这意味着创建一个类,但是这个类是一个指针吗?
但结果只是从现有类 USphereComponent 创建一个名为 ProxSphere 的对象。
这个语法的实际含义是什么?它的用法是什么?
Jts*_*Jts 12
class Someotherclass; // That has not been defined yet
class HelloWorld
{
Someotherclass* my_pointer;
};
Run Code Online (Sandbox Code Playgroud)
或者另一种选择:
class HelloWorld
{
class Someotherclass* my_pointer;
};
Run Code Online (Sandbox Code Playgroud)
如果您有多个指向尚未定义的类的指针(或引用),第一个显然是正确的。
第二个更好吗?(我不知道)如果你只需要做一次,否则做
class HelloWorld
{
class Someotherclass* my_pointer;
class Someotherclass* my_pointer2;
class Someotherclass* my_pointer3;
void func(class Someotherclass* my_pointer, class Someotherclass& my_ref);
};
Run Code Online (Sandbox Code Playgroud)
可能不是最好的。
Jts的回答是正确的。我想为其添加一个用例:
这主要在有循环类依赖时使用。
喜欢:
class A { B* binst; };
class B { A* ainst; };
Run Code Online (Sandbox Code Playgroud)
由于 B 之前未知,因此无法编译。
因此,您首先要声明 B 类。
class B;
class A { B* binst; };
class B { A* ainst; };
Run Code Online (Sandbox Code Playgroud)
或者如前所述,您可以使用语法糖:
class A { class B* binst; };
class B { A* ainst; };
Run Code Online (Sandbox Code Playgroud)
这种依赖可能是一种代码味道。这也可能是可以的,甚至是必要的。如果你有它,你应该仔细考虑是否不能通过其他方便的方式来做到这一点。
| 归档时间: |
|
| 查看次数: |
1641 次 |
| 最近记录: |