[英]How to initialize an object that declared as const on header file?
[英]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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.