Thread: initialization lists

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    initialization lists

    i have searched many sources about initialization lists but i can't find a useful source . can't understand what it is and for what it is used .Can Anybody explain it in details or show me a good source? i read from http://www.cprogramming.com/tutorial...lists-c++.html

    but couldn't understand

    moreover can you explain that code step by step with all details(i dont know copy constructors and assignment operators)..Please don't pass any point .Thanks.
    Code:
    struct Base
    {
    int x;
    Base ( int y, int z ) : x ( y+z ) 
    {
    // list above is the same as x=y+z; but it calls the constructor for x instead of the assignment operator
    }
    };
    
    struct Derived : public Base
    {
    double d;
    Derived ( int y, double d ) : Base ( x, 5 ), d ( d )
    {
    // Above calls Base constructor and initializes d
    // Notice that the d outside parentheses is the struct member,
    // the one inside parentheses is the constructor parameter
    }
    }
    Last edited by Salem; 03-09-2011 at 08:45 AM. Reason: Add

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    What is a constructor?The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.
    Syntax:
    Code:
    <class name> { arguments};
    What is copy Constructor?This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.
    For example to invoke a copy constructor the programmer writes:
    Code:
    Exforsys e3(e2); 
    or 
    Exforsys e3=e2;
    Example:
    Code:
    #include <iostream.h>
    class Exforsys
    {
        private:
            int a;
        public:
            Exforsys()
            { }
            Exforsys(int w)
        {
            a=w;
        } 
        Exforsys(Exforsys& e)
        {
            a=e.a;
            cout<<” Example of Copy Constructor”;
        }
        void result()
        {
            cout<< a;
        } 
    };
    
    
    
    void main()
    {
        Exforsys e1(50); 
        Exforsys e3(e1);
        cout<< “ne3=”;e3.result();
    }
    In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is
    Code:
    Example of Copy Constructor
    e3=50
    Important things about constructors:
    1. A constructor takes the same name as the class name.
    2. The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
    3. No return type is specified for a constructor.
    4. The constructor must be defined in the public. The constructor must be a public member.
    5. Overloading of constructors is possible. This will be explained in later sections of this tutorial.
    What is assignment operator?
    Code:
    int x;
    x=5;
    So, = is the assignment operator.
    Rule:The operand at the right always shifts to left operand.
    What is member initialization list?
    In C++, whenever an object of a class is created, its constructor is called. But that's not all--its parent class constructor is called, as are the constructors for all objects that belong to the class. By default, the constructors invoked are the default ("no-argument") constructors. Moreover, all of these constructors are called before the class's own constructor is called.
    Initialization lists or "initialization lists" are executed at the very beginning of the construction of an object, before the body of the constructor is entered. They are used to initialize the values in the object. The occasions where initialization lists cannot be used is in the initialization of arrays as data members. If you have constants or references in your class, they can only be initialized in the initialization list. So, initialization of members is must in this case.
    Code:
    class example
    {
       const int MAX_SIZE;
       int& ref;
    
    public:
       example(int& r):MAX_SIZE(128), ref(r) {}
    
       // this is wrong
       // example() {MAX_SIZE = 128; ref = r; }
    };
    The objects are initialized in the order they are declared in the class.
    I hope this will help you alot.....

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    4 questions too

    Firstly thanks for all your effort for helping.I generally understand but I have some questions too.
    1. when we use pass by value ,at the end of the func. ,all changes which is done in the func. body are terminated.for example:
    void f(x)
    {
    x=2x
    }
    int main()
    {
    f(5);
    cout<<x;
    } gives output 5.

    then, what is the situation for constructor ?Is it same for constructors?

    do you use the & symbol for that reason(permanent change) in this constructor: Exforsys(Exforsys& e)

    2.

    Exforsys(int w)
    {
    a=w;
    }
    Exforsys(Exforsys& e)
    {
    a=e.a;
    cout<<” Example of Copy Constructor”;
    }

    which constructors are copy constructor?
    how can i know the one is copy constructor?

    3.
    you said "The objects are initialized in the order they are declared in the class. "

    in the class ,MAX_SIZE declared firstly and then ref secondly.also,MAX_SIZE is initialized firstly and then ref secondly.So there is in correct order,isnt it?There is a misunderstood...?

    class example
    {
    const int MAX_SIZE;
    int& ref;

    public:
    example(int& r):MAX_SIZE(128), ref(r) {}

    // this is wrong
    // example() {MAX_SIZE = 128; ref = r; }
    };


    4.i know c.But i haven't seen such a use:
    int& ref;

    what is this?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    1) You pass the value of that argument, not the argument itself. Every variable/constant can be used only after it's been declared and only in the code block {} that it's been declared.

    In your example, you'll get lots of compiler errors!

    2) A copy constructor is one that takes one parameter of the same type as *this. ( Preferably call by constant reference "sometype(const sometype&)" ). Its duty is ... to copy each of the passed objects' elements to *this.

    3) Yes, you're correct about that.

    4) "int& ref" was introduced with C++. It's a reference. It allows you to manipulate another value through this one. Something like an indirect pointer.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    1. i don't write the whole code .i write some parts in order to explain the code which exists in my mind.You are right.i changed it and the whole part is:

    #include <iostream>

    void f(int);
    int main()
    {
    int a=5,b=10;
    while(b--)
    f(a);

    printf("%d",a);
    return 0;

    }

    void f(int x)
    {
    x++;
    }


    function f is called ten times ,but the value of a doesn't change because of call by value.If i use call by reference it ,a would be 15.It is a rule for functions .And my question is : are same things valid for constructors? If i call a constructor with call by value,will changes be terminated after constructor exits?


    2.ok.

    3.which is correct ?Mr. 777 write the code incorrect or i misunderstood.which one is true?

    class example
    {
    const int MAX_SIZE;
    int& ref;

    public:
    example(int& r):MAX_SIZE(128), ref(r) {}

    // this is wrong
    // example() {MAX_SIZE = 128; ref = r; }
    };

    you said "The objects are initialized in the order they are declared in the class. "

    in the class ,MAX_SIZE declared firstly and then ref secondly.also,MAX_SIZE is initialized firstly and then ref secondly.So there is in correct order.If so the code followed by // must be true.But Mr77 said that this is wrong. ?i am confused??

    4.Can you explain it with a easy example or examples?
    Last edited by loves_oi; 03-09-2011 at 01:34 PM.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    please use [ code ] and [ /code ] tags, it is the standard for the boards. if you need to learn how, goto the stcky at the top

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Lists? Is it possible? Is it a logical solution?
    By arcaine01 in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2009, 01:08 AM
  2. Regarding const array initialization
    By codegeru in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2009, 10:55 AM
  3. initialization lists in header files?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 10:20 PM
  4. Derived classes and initialization lists
    By DominicTrix in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2004, 05:15 PM
  5. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM