initialize constant and initialization list

This is a discussion on initialize constant and initialization list within the C++ Programming forums, part of the General Programming Boards category; I learned that we can not initialize const member in class constructor.WE must initialize it in initialization list. I don't ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    initialize constant and initialization list

    I learned that we can not initialize const member in class constructor.WE must initialize it in initialization list.

    I don't understand the magic behind this rule.What are the differences between initializing a member in initialization list and in constructor?
    Last edited by sawer; 07-08-2006 at 04:31 AM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I was curious about the same thing before, so I asked my lecturer, and he said something about it being better or faster or more efficient or something to do it in the member initialisation list. I think he mentioned something to about it being better if one was to have classes within classes within classes, etc, but seeing as I didn't forsee any real position where this may happen (to me at this and that time), I don't/didn't generally use those lists. In fact I had forgotten about them. I normally have a setter method, and use that in the constructor to save on code space.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    My understanding is that inside the constructor body a valid object is already constructed ( all the member variables are created ) so it is not initialisation but assignement what you would do to any membervariables, and it's illegal to assign a value to a const member.
    Kurt

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    The answer is in understanding the difference between initialization and assignment. For some objects you don't have any option other than initializing the object in the initialization list.

    First, the name object is not restricted to a class or struct. An object is any name. So, int value = 0; declares and initializes an object named value.

    Anyways, here is the thing:

    An object needs to be initialized in the initialization list when it either doesn't provide a default constructor or it is a const object. Lets go back to my first paragraph and work with a const object to better understand the difference.

    Const objects cannot be changed. An implication of this rule is that they must be initialized when they are defined.

    Code:
    const int value = 12;   // correct.
    const int val;  // compile-time error. const objects need to be initialized
    value = 23;  // compile-time error. Attempting to assign to a const object
    You initialize an object when you first assign to an object. You don't initialize an object on any subsequent assignment.

    Code:
    int value = 12;  // object defined and initialized
    int val;  // object defined but uninitialized (see below)
    value = 23;  // object assignment. Not initialization
    val = 13;  // object initialized.
    There is an important distinction for objects of built-in type, like the ones above, when defined on the global scope. The val object above is assumed to have been defined on the local scope (inside main() or any other function). Objects of built-in type defined in the global scope are usually zero-initialized. "Usually", because it depends on your compiler. So, had val been defined outside main(), it would have been in fact initialized to 0. The expression val = 13; you see after that would then have been an assignment, not a initialization.

    For the most part this distinction between assigment and initialization is not important. But it becomes so when working with classes and initialization lists.

    The initialization list in a class constructor declaration, like the name itself implies, initializes the class members. The body of the constructor, on the other hand, assigns to them.
    Code:
    // Correct. const_val and val initialized
    class MyClass {
    public:
        MyClass(int x, int y): const_val(x), val(y) {}
    
        void get_data() { std::cout << const_val << " " << val << std::endl; }
    private:
        const int const_val;
        int val;
    };
    
    // Correct const_val initialized and Non const of built-in type assigned.
    class MyClass {
    public:
        MyClass(int x, int y): const_val(x) { val = y; }
    
        void get_data() { std::cout << const_val << " " << val << std::endl; }
    private:
        const int const_val;
        int val;
    };
    
    //Compile-time error. Attempting to assign to const_val.
    class MyClass {
    public:
        MyClass(int x, int y): val(y) { const_val = x}
    
        void get_data() { std::cout << const_val << " " << val << std::endl; }
    private:
        const int const_val;
        int val;
    };
    And that's about it. If an object that doesn't define a default constructor is made a data member of another class, it must also be initialized in the initialization list.
    Code:
    class SomeObject {
    public:
        SomeObject(int x): value(x) {} // non-default constructor
    private:
        int value;
    };
    
    class UsingSomeObject {
    public:
        UsingSomeObject(SomeObject obj) { value = obj; } // Compile-time error.
    private:
        SomeObject value;
    };
    Last edited by Mario F.; 07-08-2006 at 06:12 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    thanks friends.
    thanks Mario F., now i undersand it, this is perfect answer.
    Good works.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just one small addition to the answer. Remember that not putting an object in the initialization list (or not having an initialization list at all) is the same as putting it there with the default constructor (except for primitives).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File Arrays
    By Yobbo in forum C Programming
    Replies: 3
    Last Post: 09-08-2005, 01:39 AM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  3. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  4. Replies: 2
    Last Post: 12-25-2001, 03:18 PM

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