Define const object in a header file

I have a question concerning the const keyword in C++. I have the following class:

Foot.h

class Foot
{
public:
   Foot (bool isRightFoot);
   const Vector internFootAxis;
   const Vector externFootAxis;
   bool isRightFoot;
private:
   Body* body;
}

Where Vector is a class implementing basic R^3 vector operations. internFootAxis denotes the vector which goes from the center of the foot (represented as a Body object - this is a class representing a physical object) to the big toe. externFootAxis denotes the vector which goes from the center of the foot to the little toe.

I want the initial value of internFootAxis and externFootAxis to be const (because I am applying operators at each iteration in the graphical display main loop to those vectors which change the inner state of the vector). Unfortunately, the values of internFootAxis(t=0) and externFootAxis(t=0) depends if I am considering the left or right foot, therefore I need to declare their values inside the constructor of Foot and not outside the class.

Technically I want to do the 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 a simple way to do that ? Thanks a lot for your help

V.

Answers


Use initializer lists:

Foot::Foot(bool isRightFoot)
  : internFootAxis( 1, isRightFoot ? 1 : -1, 0)
  , externFootAxis( 1, isRightFoot ? -1 : 1, 0)
{
    body = new Body();
}

Need Your Help

Multithread issue in JBoss 7

java multithreading spring hibernate jboss7.x

I have got a problem (or doubt) about multithreading in jboss 7. My situation is more or less simillar to situation described here:I have got piece of code that is running in many threads and now I...

Why are function pointers and data pointers incompatible in C/C++?

c++ c pointers function-pointers

I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn't both be simply addresses into ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.