Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14841
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-08 14:28
文章分类

全部博文(11)

文章存档

2013年(11)

我的朋友

分类: C/C++

2013-08-22 21:21:22

Example:

  1. #include <stdafx.h>
  2. #include <iostream>
  3. using namespace std;

  4. class Base{
  5. public:
  6.     Base(){
  7.         cout<<"Base()"<<endl;
  8.     }
  9.     Base(int){
  10.         cout<<"Base(int)"<<endl;
  11.     }
  12.     ~Base(){
  13.         cout<<"~Base()"<<endl;
  14.     }
  15.     Base& operator=(const Base& x){
  16.         cout<<"Base::=(const Base& x)"<<endl;
  17.         return *this;
  18.     }
  19.     Base& operator=(int x){
  20.         cout<<"Base::=(int x)"<<endl;
  21.         return *this;
  22.     }
  23. };
  24. class Derive{
  25. public:

  26. };
  27. int main(){
  28.     Derive x;
  29.     Derive y(1); //error
  30.     Derive z;
  31.     x=z;
  32.     x=1; //error
  33.     return 0;
  34. }
非自动继承的函数包括父类的构造函数,析构函数和复制操作符=,注意,此处虽然是非自动继承,但并不妨碍派生类自动调用父类的构造函数Base()和析构函数~Base()。另外要注意,虽然重定义一个类的构造函数之后,编译器不会再生成默认构造函数,但重定义赋值操作符且参数是其他类型时,编译器仍然回升诚意个标准的复制操作符。
阅读(254) | 评论(0) | 转发(0) |
0

上一篇:子类覆盖基类成员函数

下一篇:没有了

给主人留下些什么吧!~~