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
相关文章推荐
- Difference Between Initialization and Assignment in C++
- Aggregrate:Initialization of structures and arrays in C++
- Constructor and destructor -- Initialization & Cleanup in C++
- Understanding and Implementing Observer Pattern in C++
- Description of CWnd derived MFC objects and multithreaded applications in Visual C++
- Adding and Removing Routes in the Linux Routing Table in C/C++
- RAII and smart pointers in C++ from stackflow
- Sublime Text 2 for C/C++ and Python in Windows
- Why Not Mix Signed and Unsigned Values in C/C++?
- how to save a c++ object in java object and use it
- Command Line Applications with Visual Studio 2008 and C++ Posted by omniplex in Errors
- Sun Studio C++ - comilation error in stdexcept and iostream.
- Leetcode_swap-nodes-in-pairs(c++ and python version)
- Mixing C and C++ Code in the Same Program
- Enabling and Disabling Privileges in C++
- common and different of final in java and const in C++
- operator in C and C++
- Where Are ClassWizard and WizardBar in Visual C++ .NET?[msdn]
- Overload and override in c++
- Secure Coding Best Practices for Memory Allocation in C and C++