• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

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;
}

0
yakky
Asked:
yakky
  • 5
  • 2
  • 2
  • +1
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now