Initializing constants

This is a discussion on Initializing constants within the C++ Programming forums, part of the General Programming Boards category; Someone holler back at me!! Constants cannot or should not be modified, right? Let's suppose you have class Increment { ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Question Initializing constants

    Someone holler back at me!!

    Constants cannot or should not be modified, right?

    Let's suppose you have

    class Increment {
    public :
    Increment(int c=0, int 1=1); // non-const constructor
    ....
    ....
    ......

    private:
    int count;
    const int increment; //const data member
    };

    Increment::Increment(int c, int i)
    : increment (i) // initialize constant member
    {count =c;}


    How is this contstant being modified?
    by and to what?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    const variables need to be declared with a constant value at the time of compilation, not at run time:

    int i being passed as a variable and used to initialize the value of increment is not a constant.
    Last edited by elad; 02-28-2003 at 02:36 PM.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by elad
    const variables need to be declared with a constant value at the time of compilation, not at run time:

    int i being passed as a variable and used to initialize the value of increment is not a constant.
    Not true. The datamember is still considered constant even though there will most-likely be no way for the system to realize its "constness" at runtime. Attempting to explicitly alter the variable from within an object will cause a compile-time error.

    To answer your question, ddavidson, the constant ISN'T being modified. It'd just being initialized. In you class there is a separate const object per datatype. Only one value is assigned to the const data during its life time. You may be confusing a const datamember with a static const datamember.

    Also, there is no such thing as a "const constructor" because construction of the object is not complete at the call of the object (meaning it's not constant at this point -- in fact, it's not even considered an object of the type being constructed yet). A constructor of a const object and a non-const object are therefore exactly the same.
    Last edited by Polymorphic OOP; 02-28-2003 at 03:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variables and constants.
    By dave1339 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  4. Numerics to defines constants??
    By xjcass in forum Windows Programming
    Replies: 4
    Last Post: 11-15-2003, 12:23 PM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21