您的位置:首页 > 编程语言 > C语言/C++

Initialization and assignment in C++

2017-10-20 12:25 302 查看
 

 

Whatever, the c++ compiler has supplied for a concrete object(an instance of some user-defined type), is going to be defaulted in terms of the nature of the type. Default constructor, default copy constructor and default assignment operator is all about the type. However, this kind class has to interact with other type—derived classes, base classes, any built-in type—to communicate. Overloaded member functions and operators are needed to deal with these cases, describing the details of interaction. 

 

First, focus on the right side of ‘=’ operator, an object of any type could be assigned to the object. So, many overloaded ‘=’ operators specialized have to be implemented. Then, for constructors used to initialize the object, the number of arguments could be different in terms of the context, including overloaded default constructor, overloaded copy constructor, overloaded conversion constructors and overloaded listed-arguments constructors. 

 

When program uses different types to initialize the object, different conversion constructors with corresponding type argument is invoked to create the new object. By contrast, copy constructor is invoked when the type is same with the caller and its derived class.

 

Programmer can implement many overloaded constructors and assignment operators to cope with those odd-looking demands.  

 

          

Initialization:

1,create an object          

2, return an object in a function by value     

3, transmit arguments by value  

 

Assignment:

1,

 

Constructors:             0-argument    1-argument   >=2-arguments

 

1, default constructor  (can be overloaded)   

    ex: Classname();

 

2, default copy constructor   (can be overloaded)

    ex: Classname(Classname &I);

 

3, other overloaded constructors   

    ex, conversion constructors(with only one argument), 

    ex, constructors with list arguments( >= 2) , 

Assignment operators:      the type of right-side of = operator can be any type  

 

1, default assignment(can be overloaded )       

   ex : Classname object1 =  object2;

 

2, other overloaded assignments   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: