const members

Could someone please explain to me how to create constant members. i tried initializing the constant in the constructor, but the compiler complains and requests that i initialize the member in the constructor base/member initializer list, what does this mean??

cpu4ghzAsked:
Who is Participating?
 
mnashadkaCommented:
The member initialization list is the section in the constructor right after the colon (:).  It would look something like:

// A.h
class A
{
public:
  A();
private:
  const x;
};

// A.cpp
A::A()
: x(321) // Initialize in the member initialization list
{
}

Hope this helps.
0
 
cpu4ghzAuthor Commented:
Thanks alot for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.