在头文件中定义const对象
Define const object in a header file
我有一个关于c++中的const关键字的问题。我有以下类:
Foot.h
class Foot
{
public:
Foot (bool isRightFoot);
const Vector internFootAxis;
const Vector externFootAxis;
bool isRightFoot;
private:
Body* body;
}
其中Vector是实现基本R^3向量运算的类。internFootAxis表示从脚的中心(表示为Body对象-这是一个表示物理对象的类)到大脚趾的向量。externFootAxis表示从脚中心到小脚趾的矢量。
我希望internFootAxis和externFootAxis的初始值为const(因为我在图形显示主循环的每次迭代中应用操作符来改变向量的内部状态)。不幸的是,internFootAxis(t=0)和externFootAxis(t=0)的值取决于我是考虑左脚还是右脚,因此我需要在foot的构造函数中声明它们的值,而不是在类之外。
技术上讲,我想做以下操作
Foot.cpp
Foot::Foot(bool isRightFoot)
{
body = new Body();
if (isRightFoot)
{
internFootAxis(1,1,0);
externFootAxis(1,-1,0);
}
else
{
internFootAxis(1,-1,0);
externFootAxis(1,1,0);
}
}
有简单的方法吗?非常感谢你的帮助
v .
使用初始化器列表:
Foot::Foot(bool isRightFoot)
: internFootAxis( 1, isRightFoot ? 1 : -1, 0)
, externFootAxis( 1, isRightFoot ? -1 : 1, 0)
{
body = new Body();
}
相关文章:
- 被解释为低级别const的const对象的地址
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- "this"指向的对象是否与 const 对象相同?
- 从 const 对象访问非 const 方法
- 将非 const 对象用于 const 参数
- 如何键入用于const对象的自定义io操纵器
- 哪个 const 与哪个引用将更改参数并创建一个 const 对象?
- 无法在返回中转换 const 对象
- 接受对 const 对象的引用但没有 r 值的函数
- 当我调用 main 中使用 const 对象的 const 函数时,不断出现错误
- 在 const 对象上调用非 const 函数
- 正确的方法通过巨大的const对象的向量
- 如何重载 const 对象的 [],以便值赋值不会导致编译错误
- 相互引用的 Const 对象
- 析构函数是否可以在 const 对象上调用非 const 函数
- decltype 不会推断 const 对象的 const 成员
- 防止const函数被调用非const对象
- constexpr引用非const对象