Simple Question

Nov 4, 2012 at 11:09am
Let's say i'm creating a class and i want to declare one of my "char" private variables to be "const", how can I change its content later on in the constructor?
I know if i have a const int variable, we put : after the constructor

1
2
3
4
5
6
7
8
9
10
11
class Example{
private:
const int variable;

public:
Example(int v):variable(v)
{
  variable = v;
}
};


BUT THIS WONT WORK WITH char. Any help ?? Thanks
Nov 4, 2012 at 11:13am
Your example should work if you remove line 8.
Nov 4, 2012 at 11:18am
You have to initialize constant variables in the initialization list, because they need to have their value set during construction. Chars work the exact same way as ints in this case.
Nov 4, 2012 at 11:22am
Peter87 Thank you, i removed line 8 and it worked perfectly for char :D

thanks blacksheep :)
Topic archived. No new replies allowed.