Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About member constants 1

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
SG
I am reading The C++ Programming Language, 3rd Ed. book by Bjarne Stroustrup. It is really a nice book. However there are some parts that are unclear to me. It is about member constants that can be found at section 10.4.6.2 of the same book.

I will post the code here:
Code:
class Curious {
public:
  static const int   c1 = 7    ;  // ok, but remember definition
(1)
Code:
static int         c2 = 11   ;  // error: not const
(2)
Code:
const  int         c3 = 13   ;  // error: not static
(3)
Code:
static const int   c4 = f(7) ;  // error: in-class initializer not constant
(4)
Code:
static const float c5 = 7.0  ;  // error: in-class not integral[code]    [b](5)[/b]
[code]} ;


Now I know that there are comments but what I am looking for is the reason why those marked with errors does not work. I'd like to know why initialization of a const member variable in item 3 is not allowed if it is not static. The same type of questions is what I want to ask for items 4 and 5.

I tried to look it up at ISO/IEC 14882:1998(E) but since it was to long, I gave up looking. If there is anyone here could answer my questions above, please do help me and if you can, kindly point me to the section in 14882 or to any online reference.

Thanks!

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Item 3: Since the variable is not static, there are actually multiple copies of the variable (one for each instance of the class you create). This doesn't work for the simple reason that you would never have a reason for wanting it to work. Since each instance of the class will have this constant, it will still be unchanging, but it can (and probably will) be different for each Curious object you create, so there'd be no point in initializing it to one value. Of course, you'd have to initialize the constant in the constructor each time you create a Curious object. If you wanted the variable to be the same for each Curious object you created, you'd be using a static variable anyway.

Item 4: This doesn't work because, since it's a compile-time constant, you by definition need to have the value at compile time. You can only run the function at run time. Since you need to run the function to get the compile-time constant... it just doesn't work.

Item 4: You can only initialize integral compile-time constants in a class definition. Floating point numbers are not integral. I believe, however, you can omit the initialization of a float inside the class, and initialize it outside the class when you define the static variable.
 
Code:
static const int   c1 = 7    ;
//(1) Const static can be given a value in it's declaration.

static int         c2 = 11   ;  
//Just because it's static doesn't make it const, 
//the value needs to be assigned inside of a member function

const  int         c3 = 13   ;  
//Justas said before, there are more than one copy of this
//variable... One for each instantated object of this type

static const int   c4 = f(7) ;  
//Requires a function call, so the assignment won't happen
//untill runtime... When f()'s return value can be
//calculated.
    
static const float c5 = 7.0  ;  
//floats are a bit more intresting than ints...
//This should be saved for inside one of the member
//functions of the class.  Floats are handeled by a co-
//prossessor at runtime...

If your ever truly curious about it, compile short little programs and examine the assmbly output... Especially to questions that have answers like "That's just the way the system works." The thing you'll find, other than theat assembly is a pain, is that the underlying architecture of the system has a lot to do with the counter intuitive things like item 5.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top