IDentity* 本地实体指针示例代码片段
IEntity* wLocalEntity pointer example code snipet
此 C++ 代码片段的作用是什么?
IEntity* wLocalEntity= const_cast<IEntity*>(BaseSimSystem::getEntityRef());
if(wLocalEntity!=0){
mEntitySpeed=wLocalEntity->getSpeed();
}
我不确定它与模板创建有什么关系。有人可以向我解释这段代码的作用吗?
谢谢。
这是代码。
IEntity* wLocalEntity= const_cast<IEntity*>(BaseSimSystem::getEntityRef());
if(wLocalEntity!=0){
mEntitySpeed=wLocalEntity->getSpeed();
}
如果你真的被问及这段代码,我会做的第一件事就是抱怨 if 子句。 0 应该是 nullptr:
if (wLocalEntity != nullptr) {
mEntitySpeed = wLocalEntity->getSpeed();
}
此外,请告诉我您知道不应该如此严格地压缩代码。当您将所有这些运算符推到一起且没有空格时,错误会隐藏起来。
现在,让我们看一下第 1 行:
IEntity* wLocalEntity = const_cast<IEntity*>(BaseSimSystem::getEntityRef());
显然,wLocalEntity 是指向 IEntity 的指针。我希望你明白这一点。
const_cast<>
位很荒谬,可能是一个错误。我们不知道 BaseSimSystem::getEntityRef() return 是什么,但我怀疑它 return 是一个 const 指针,现在您正试图将其分配回一个非常量变量。 const_cast<> 正在摆脱常量。
正确的代码几乎可以肯定是:
const IEntity * wLocalEntity = BaseSimSystem::getEntityRef();
但是,IEntity 的方法可能没有真正应该的标志 const,因此您可能不得不这样做,因为其他一些程序员没有在他应该有的时候应用 const。
所以 const_cast<IEntity *>
说“取括号中的 return 值,是的,我知道它们不是非常量 IEntity *,但不要警告我,因为应该我知道我在做什么。
怎么样?
此 C++ 代码片段的作用是什么?
IEntity* wLocalEntity= const_cast<IEntity*>(BaseSimSystem::getEntityRef());
if(wLocalEntity!=0){
mEntitySpeed=wLocalEntity->getSpeed();
}
我不确定它与模板创建有什么关系。有人可以向我解释这段代码的作用吗? 谢谢。
这是代码。
IEntity* wLocalEntity= const_cast<IEntity*>(BaseSimSystem::getEntityRef());
if(wLocalEntity!=0){
mEntitySpeed=wLocalEntity->getSpeed();
}
如果你真的被问及这段代码,我会做的第一件事就是抱怨 if 子句。 0 应该是 nullptr:
if (wLocalEntity != nullptr) {
mEntitySpeed = wLocalEntity->getSpeed();
}
此外,请告诉我您知道不应该如此严格地压缩代码。当您将所有这些运算符推到一起且没有空格时,错误会隐藏起来。
现在,让我们看一下第 1 行:
IEntity* wLocalEntity = const_cast<IEntity*>(BaseSimSystem::getEntityRef());
显然,wLocalEntity 是指向 IEntity 的指针。我希望你明白这一点。
const_cast<>
位很荒谬,可能是一个错误。我们不知道 BaseSimSystem::getEntityRef() return 是什么,但我怀疑它 return 是一个 const 指针,现在您正试图将其分配回一个非常量变量。 const_cast<> 正在摆脱常量。
正确的代码几乎可以肯定是:
const IEntity * wLocalEntity = BaseSimSystem::getEntityRef();
但是,IEntity 的方法可能没有真正应该的标志 const,因此您可能不得不这样做,因为其他一些程序员没有在他应该有的时候应用 const。
所以 const_cast<IEntity *>
说“取括号中的 return 值,是的,我知道它们不是非常量 IEntity *,但不要警告我,因为应该我知道我在做什么。
怎么样?