# constant inetegers

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Sorry about this, it is very very newbie but my mind has died and can't seem to figure it out. Basically i have a class and want to declare a meember variable constant so i have this - but i am not sure it is right - basically what i want is to instantiate a person with a supportiveness value and have it so that value can never change. class Person { Private: const int Supportiveness; Public: Person(); ~Person(); int getPersonSupportiveness() {return Supportiveness;} void setPersonSupportiveness (int Person Supportiveness) {Supportiveness = PersonSupportiveness;} }; or do i just do a public variable that is a constant or none of the above. Sorry for the idiocy but at the moment i can't help it!! EDIT: I have edited it to show a fuller c++ declaration. I would normally use the get and set functions as shown above. Only thing is if i have a set function can i keep the value constant or will this override the value and change it. My aim is that once the value is set it will never change. Neil

##### Share on other sites
What language is this? It looks like a C++-style language, but the syntax doesn't match C++ or C#.

##### Share on other sites
If I understand what you're asking you can do:
class Person {  public:    Person(int support_value)       : supportiveness_(support_value)    {}    int get_supportiveness(void) {      return supportiveness_;    }  private:    const int supportiveness_;};

##### Share on other sites
Well that should work just fine, have you tried it?

class Person

{

private:
const int Supportiveness;

public:
int getPersonSupportiveness () {return Supportiveness;}

};

EDIT: lowercased private and public (not sure if it matters but its good form).

Quote:
 Person(int support_value) : supportiveness_(support_value) {}

What is this suppose to be doing? Its a const (cant change)

##### Share on other sites
no i hadn't tried it - pen and paper work at the moment thats why i was checking.

I've added the set bit too it now, otherwise it would have to be instantiated in the constructor (which it isn't), so does that look ok - or will that now mean that its possible to change the supportiveness number calling the setSupportiveness function?

##### Share on other sites
You cant change a const. it a constant. so if you drop the const you can do the set and all. I think you want to drop the const know that I think about it. Supportive I think would change.

##### Share on other sites
Quote:
Original post by slymr
Quote:
 Person(int support_value) : supportiveness_(support_value) {}

What is this suppose to be doing? Its a const (cant change)

Initializing the const member variable.

##### Share on other sites
Quote:
Original post by slymr
...
Quote:
 Person(int support_value) : supportiveness_(support_value) {}

What is this suppose to be doing? Its a const (cant change)

Any constant variables in a class must be initialized in the initializer list as they are constant and cannot change.

##### Share on other sites
Alright thanks you guys. I did not learn too much about initializer. The resources were not clear to me. Should it be like this.

Person(int support_value)

:: supportiveness_(support_value)

{}

Only one colon.

##### Share on other sites
Quote:
 Original post by slymrAlright thanks you guys. I did not learn too much about initializer. The resources were not clear to me. Should it be like this.Person(int support_value):: supportiveness_(support_value){}

: denotes the start of an initializer list, this is optional and only possible in the constructor. You seperate different items with ',' so a class could look like this:
class foo{  int a;  float b;public:  foo() : a(5),b(5.2)  {}};

I don't think anyone has mentioned it, but your setPersonSupportiveness function won't work because you can't set the value of a const variable (it won't just not set it, it will throw an error at compile-time), and because the parameter is incorrect (you need to remove the whitespace between Person and Supportiveness).

##### Share on other sites
Quote:
Original post by CTar
Quote:
 Original post by slymrAlright thanks you guys. I did not learn too much about initializer. The resources were not clear to me. Should it be like this.Person(int support_value):: supportiveness_(support_value){}

: denotes the start of an initializer list, this is optional and only possible in the constructor. You seperate different items with ',' so a class could look like this:
class foo{  int a;  float b;public:  foo() : a(5),b(5.2)  {}};

I don't think anyone has mentioned it, but your setPersonSupportiveness function won't work because you can't set the value of a const variable (it won't just not set it, it will throw an error at compile-time), and because the parameter is incorrect (you need to remove the whitespace between Person and Supportiveness).

And for the sake of completeness, you also can't set it in the constructor body; you *have to* use the initializer list. The whole idea is that this value doesn't change (that's what you promise by calling it 'const', after all), and an assignment in a function body - even the constructor - changes it. The constructor initializer list is where you get to specify the non-changing value. (Otherwise, it would be uninitialized and thus unknown, but still not changeable - not a very good way of writing programs! It doesn't even make a terribly good RNG :s )