Jump to content
  • Advertisement
Sign in to follow this  

constant inetegers

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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;}

};

add the bolded part.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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)

{}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!