943,470 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2918
  • C++ RSS
Sep 7th, 2004
1

A question about 'const'

Expand Post »
the book "Effective C++" have a item introduce cosnt,but a question i dont understand clearly.

C++ Syntax (Toggle Plain Text)
  1. class GamePlayer {
  2. private:
  3. static const int NUM_TURNS = 5; // constant eclaration
  4. int scores[NUM_TURNS]; // use of constant
  5. ...
  6. };
  7.  
  8. const int GamePlayer::NUM_TURNS; // mandatory definition;
  9. // goes in class impl.file

why must define the NUM_TRUNS in static ?
Similar Threads
Reputation Points: 15
Solved Threads: 0
Newbie Poster
XianBin is offline Offline
24 posts
since Aug 2004
Sep 7th, 2004
1

Re: A question about 'const'

const is short for constant.An constant must be initialised when it is declared and it cannot be changed.

In a class you cannot declare a member as datatype data_var = some_value since it's against standards and each object of that class has a sperate space in mem to store all it's members.

when a member is declared as static, all the objects of the class share this value and you can assign a value as static datatype data_var = some_value as you cannot do that in the constuctor as when an object is initialised this value will get overwriten.So we are allowed to do it within the class.

Helps?

p.s: look up on constructors
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Sep 7th, 2004
1

Re: A question about 'const'

Actually,const can be changed.I donnt know about c++,but in c that should be true.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
GDFans is offline Offline
4 posts
since Sep 2004
Sep 7th, 2004
1

Re: A question about 'const'

You don't HAVE to define NUM_TURNS as static. Defining it as static makes it common between all objects of that class. So if you have
GamePlayer a1;
GamePlayer a2;
then, a1.NUM_TURNS and a2.NUM_TURNS don't exist, but NUM_TURNS does.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Sep 7th, 2004
0

Re: A question about 'const'

thanks a lot
Reputation Points: 15
Solved Threads: 0
Newbie Poster
XianBin is offline Offline
24 posts
since Aug 2004
Sep 8th, 2004
0

Re: A question about 'const'

Quote originally posted by iamboredguy ...
You don't HAVE to define NUM_TURNS as static. Defining it as static makes it common between all objects of that class. So if you have
GamePlayer a1;
GamePlayer a2;
then, a1.NUM_TURNS and a2.NUM_TURNS don't exist, but NUM_TURNS does.
True,and if NUM_TURNS were public you could access it by GamePlayer::NUM_TURNS
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: include file problem
Next Thread in C++ Forum Timeline: Faulty programme!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC