The difference between the C + + constructor initialization list and the assignment in the constructor __jquery

Source: Internet
Author: User

There are two ways to initialize a member variable in a C + + class:

Constructor initializes the list and the constructor body assignment. Here's how the two approaches are different.

The order in which member variables are initialized is in the order that they are defined.

1. Internal data type (Char,int ...) pointers, etc.)

Class Animal
{public
:
    Animal (int weight,int height):       //a Initialization list
      m_weight (weight),
      m_height (height)
    {
    }
    Animal (int weight,int height)       //b function Body initializes
    {
        m_weight = weight;
        m_height = height;
    }
Private:
    int m_weight;
    int m_height;
};

for these internal types, there is basically no difference, and there is not much difference in efficiency.

Of Course A and B cannot coexist.

2, no default constructor in the inheritance relationship

Class Animal
{public
:
    Animal (int weight,int height):        //No parameterless constructors are provided 
      m_weight (weight),
      m_ Height (height)
    {
}
private:
    int m_weight;
    int m_height;
};

Class Dog:public Animal
{public
:
    Dog (int weight,int height,int type)   //error constructor Parent class Animal No proper constructor
    {
    }
private:
    int m_type;

The above subclass and parent class compilation can be faulted:


Because the subclass dog Initialize the parent class animal before initialization, but according to the dog constructor, no arguments are passed to the parent class, and the parameterless constructor animal the parent class is used. The parent class animal provides a constructor with parameters so that the compiler does not give the parent class animal a default parameterless constructor, so the compile times are wrong and say that the appropriate default constructor is not available. Either provide a constructor with no parameters, or pass initialization parameters to the parent class animal in the dog initialization list of the subclass, as follows:

Class Dog:public Animal
{public
:
    Dog (int weight,int height,int type):
        Animal (Weight,height)         //must use initialization list to increase initialization
    of the parent class {
        ;
    }
Private:
    int m_type;
};

3. The const constant in the class must be initialized in the initialization list and cannot be initialized using an assignment

Class Dog:public Animal
{public
:
    Dog (int weight,int height,int type):
        Animal (weight,height), 
        Legs (4)                //must initialize
    {
        //legs = 4 in the initialization list;           Error
    }
private:
    int m_type;
    const int legs;
};

4, member initialization that contains custom data type (Class) objects

class Food {public:food (int type = ten) {m_type = 10;
    } Food (Food &other)//copy constructor {m_type = Other.m_type;
        } Food & operator = (Food &other)//Overloaded assignment = function {m_type = Other.m_type;
    return *this;
} Private:int M_type;

};
        (1) Constructor assignment to initialize member object M_food class Dog:public Animal {public:dog (food &food)//:m_food (food) {               M_food = food;
Initializes the member object} Private:food M_food;
};
Use Food fd;   Dog Dog (FD); 

Dog Dog (FD); Result: The object type constructor Food (int type = ten)--> is then executed in the execution of the object type constructor Food & operator = (Food &other) imagine why. (2) Constructor initialization list Way class Dog:public Animal {Public:dog (Food &food): M_food (Food)//initialization member pair               
    like {//m_food = food;
} Private:food M_food;
};
Use Food fd;   Dog Dog (FD); Dog Dog (FD); Result: Execution of Food (Food &other) copy constructor completes initialization 

Different initialization methods get different results:

The obvious way to initialize a list of constructors is to be more efficient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.