Thread: initialize constant and initialization list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.
    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.

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, 04:18 PM