Item 31. Covariant Return Types & Item 32. Preventing Copying
来源:互联网 发布:eraser扣图软件 编辑:程序博客网 时间:2024/06/05 00:07
Item 31. Covariant Return Types
Covariant:随变,随动。(你变我就变)
通常,一个overriding函数必须与所重写的函数具有相同的返回类型。然而,并非事事如此,Covariant Return Types便是无需如此。如下的继承体系:
class Shape {
public:
//...
virtual Shape *clone() const = 0; // Prototype
//...
};
class Circle : public Shape {
public:
Circle *clone() const; //Covariant Return Types
//...
};
Shape 与 Circle 的clone成员函数的返回类型不同,但却可以正常运行。为何?
Circle 是 Shape 的一个子类,所以Circle*可以自动的转换成为Shape*。这就为Covariant Return Types。
Item 32. Preventing Copying
若不声明copy构造函数,编译器会自动给加上一个。而有时候不想copy构造函数被调用,如何办?
1)声明一个,但是私有的
2)声明一个,但不实现。
这两种方法都是从编译器的角度来控制的,使用了会编译不过。
- Item 31. Covariant Return Types & Item 32. Preventing Copying
- ITEM
- item
- item
- item
- item
- item
- Java covariant return type
- Covariant return type
- $.map(data,function(item,index){return XXX})
- java generic -covariant return type
- Effective C#之Item 25: Prefer Serializable Types
- Effective C#之Item 33:Limit Visibility of Your Types
- Item 3. Make copying cheap and correct for objects in containers.
- Effective C++ Item 14 在资源管理类中小心copying行为
- Effective C++ Item 14-在资源管理中小心的copying行为
- covariant return type and boost::shared_ptr
- Preventing "Return" and "Esc" From Closing Dialog
- DirectX9 3D快速上手 3
- 简单外挂制作
- Item 29. Virtual Constructors and Prototype&Item 30. Factory Method
- 我,还是那个我;她还是那个她
- 关于JSP链接数据库的问题
- Item 31. Covariant Return Types & Item 32. Preventing Copying
- VB/JAVA与单片机通信
- Ant介绍
- 《Java 手机/PDA 程序设计入门》读书笔记11-- MIDP数据库设计1
- XML指南——XML CDATA
- Web新标准:Ajax 的一些资源汇总
- 求助?WinCE的问题
- XML指南——XML编码
- XML的简单读取与写入