Copy assignment operators (C++ only)
来源:互联网 发布:手机优化软件排行榜 编辑:程序博客网 时间:2024/04/30 06:25
The copy assignment operator lets you create a new object from an existing one by initialization. A copy assignment operator of a class A is a nonstatic non-template member function that has one of the following forms:
- A::operator=(A)
- A::operator=(A&)
- A::operator=(const A&)
- A::operator=(volatile A&)
- A::operator=(const volatile A&)
If you do not declare a copy assignment operator for a class A, the compiler will implicitly declare one for you which will be inline public.
The following example demonstrates implicitly defined and user-defined copy assignment operators:
The following is the output of the above example:
The assignment x = y calls the implicitly defined copy assignment operator of B, which calls the user-defined copy assignment operator A::operator=(const A&). The assignment w = z calls the user-defined operator A::operator=(A&). The compiler will not allow the assignment i = j because an operator C::operator=(const C&) has not been defined.
The implicitly declared copy assignment operator of a class A will have the form A& A::operator=(const A&) if the following are true:
- A direct or virtual base B of class A has a copy assignment operator whose parameter is of type const B&, const volatile B&, or B.
- A non-static class type data member of type X that belongs to class A has a copy constructor whose parameter is of type const X&, const volatile X&, or X.
If the above are not true for a class A, the compiler will implicitly declare a copy assignment operator with the form A& A::operator=(A&).
The implicitly declared copy assignment operator returns a reference to the operator's argument.
The copy assignment operator of a derived class hides the copy assignment operator of its base class.
The compiler cannot allow a program in which the compiler must implicitly define a copy assignment operator for a class A and one or more of the following are true:
- Class A has a nonstatic data member of a const type or a reference type
- Class A has a nonstatic data member of a type which has an inaccessible copy assignment operator
- Class A is derived from a base class with an inaccessible copy assignment operator.
An implicitly defined copy assignment operator of a class A will first assign the direct base classes of A in the order that they appear in the definition of A. Next, the implicitly defined copy assignment operator will assign the nonstatic data members of A in the order of their declaration in the definition of A.
- Copy assignment operators (C++ only)
- C++/Copy Controls/Copy-Assignment operators
- Copy constructors, assignment operators, and exception safe assignment
- [C++]copy constructor and copy assignment operator
- 14.13 Assignment operators
- ECMAScript Assignment Operators
- Assignment operators 重载【c++ primer 】
- (Effective C++)第二章 构造、析构和赋值运算(Constructors,Destructors and Assignment Operators)
- Copy constructor and Copy assignment
- copy constructor and copy assignment
- copy 和assignment函数
- copy constructor, assignment, global
- copy-assignment operator
- Overloading unary operators (C++ only)
- Chapter2. Constructors, Destructors, and Assignment Operators
- EC之Constructors,Destructors, and Assignment Operators
- Conclusion for Constructors,Destructors,and Assignment Operators
- Copy Construct && Copy assignment(Operator =)
- 程序员眼中的女人
- VS2005中的代码注释插件
- 臭美 - 发现自己当时的文章 和心情不错嘛 哈哈
- 在ADF中使用PrimaryKey进行查询
- FCKEditor 2.x: How to add a new button with custom javascript to the toolbar
- Copy assignment operators (C++ only)
- file mode
- colortree 支持各种颜色,字体的 mfc 树控件
- Hashtable 和 HashMap的区别主要有
- SQL WHERE 1=1
- Ubuntu 9.10 Panel无响应
- Build Web Apps for iPhone using Dashcode
- VIM常用指令
- Ubuntu 9.10安装中文字体