How do I initialise a const?

Ok so Im being really dumb,
My problem is
How do I initialise this constant?

If I initialise it in the header it throws an error.
If I initialise it as shown below it thows an error.

So what is the correct way of initialising it?
Thanks in advance Yakky

class demo
{
public:
  const int myint;
  demo();
  ~demo();
};

demo::demo()
{
  myint = 5;
}

yakkyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kgreddyCommented:
I think you have to initialise the constant variable, at the
time of declaration.

You can say
const int myint = 10;
0
nietodCommented:
There are a bunch of issues to consider here.   You should be able to initialize in the constructor's initiailization list (not in the body), like

demo::demo()  :
   myint(5)
{
}

continues.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Excuse me.  I'm beta testing the new version of EE and a bug just occured.  I will be back to finish this in a few minutes.  As I said, there are a bunch of issues in this question.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ToronadoCommented:
Hi yakky

Use this :

demo::demo() : myint (5)
{
}

It is just like calling constructors from baseclasses. In fact, you are calling the construct function from the integer.
0
nietodCommented:
I'm back.

Now, what this does for you is to allow you to define a constant data member inside an object of a class.  This means that each object fo the class has its own copy of this constant data member and therefor may have different values for tor this data member, for example if you did

class Demo
{
   const int MyInt;
public:
   Demo(int i) :
      MyInt(i)
    {
    };
};

Demo A(1);
Demo B(2);

A would have MyInt set to 1 and B woudl have it set to 2.  Note that MyInt would still be constant, that is, it could not "legally" be changed in either A or B after initialization.

But I suspect that this is not what you want.  I suspect you want one constant that is shared among the objects of the class.  (i.e. one constant that is the same for all objects in the class.)  For that you need to use a static int or an enum.  Is that what you want?
0
yakkyAuthor Commented:
Yes I wanted every instance of the class to have the same number how do I do that?
0
ToronadoCommented:
Hi yakky,

Like nietod mentioned, use a static.

class demo
{
public:
  static int myint;
  demo();
  ~demo();
};

int demo::myint = 5;

demo::demo()
{
}

demo::~demo()
{
}

0
nietodCommented:
According to the standard, you can declare a static constant in the class and initialze the static constant soemwhere (The initialization must occur outside the scope of the class and may occur only one time. so it would not usually be placed inside the class's .h file.)  Like

// in a .h file.   (or in a  .cpp file.)
class Demo
{
   static const int MyConst;
public:
};

// Not in a .h file.
const int Demo::MyConst = 5;

However, not all compilers support this yet--although it is standard C++.  In addition, having to initialize the static only one time is sometimes a pain (because it can't go in the .h file.)  So another aproach is to use an enum nested in the class instead, like

class Demo
{
   enum { MyConst = 5 };
};

The advantage is that the constant value may be specified righ in the class declaration in the .h file.  the dissadvantage is that you can't specify the data type used to store the constant.  i.e you can't make it an unsigned int, for example.  And this approach only works with numerical constants.  The other approach could be used for any constant, like a pointer, or a class.
0
nietodCommented:
toronado, that is close, but it is not constant.  So there is one value that is shred among the objects, but that value may be changed.  You just need to add the "const" keyword.
0
yakkyAuthor Commented:
Thankyou very much

Yakky
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.