c++ - Define const object in a header file -
i have question concerning const keyword in c++. have following class:
foot.h
class foot { public: foot (bool isrightfoot); const vector internfootaxis; const vector externfootaxis; bool isrightfoot; private: body* body; }
where vector class implementing basic r^3 vector operations. internfootaxis denotes vector goes center of foot (represented body object - class representing physical object) big toe. externfootaxis denotes vector goes center of foot little toe.
i want initial value of internfootaxis , externfootaxis const (because applying operators @ each iteration in graphical display main loop vectors change inner state of vector). unfortunately, values of internfootaxis(t=0) , externfootaxis(t=0) depends if considering left or right foot, therefore need declare values inside constructor of foot , not outside class.
technically want following
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); } }
is there simple way ? lot help
v.
use initializer lists:
foot::foot(bool isrightfoot) : internfootaxis( 1, isrightfoot ? 1 : -1, 0) , externfootaxis( 1, isrightfoot ? -1 : 1, 0) { body = new body(); }
Comments
Post a Comment