A question about 'const'

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 24
Reputation: XianBin is an unknown quantity at this point 
Solved Threads: 0
XianBin XianBin is offline Offline
Newbie Poster

A question about 'const'

 
1
  #1
Sep 7th, 2004
the book "Effective C++" have a item introduce cosnt,but a question i dont understand clearly.

  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 ?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: A question about 'const'

 
1
  #2
Sep 7th, 2004
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
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 4
Reputation: GDFans is an unknown quantity at this point 
Solved Threads: 0
GDFans GDFans is offline Offline
Newbie Poster

Re: A question about 'const'

 
1
  #3
Sep 7th, 2004
Actually,const can be changed.I donnt know about c++,but in c that should be true.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Re: A question about 'const'

 
1
  #4
Sep 7th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 24
Reputation: XianBin is an unknown quantity at this point 
Solved Threads: 0
XianBin XianBin is offline Offline
Newbie Poster

Re: A question about 'const'

 
0
  #5
Sep 7th, 2004
thanks a lot
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: A question about 'const'

 
0
  #6
Sep 8th, 2004
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
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC