Example:
-
#include <stdafx.h>
-
#include <iostream>
-
using namespace std;
-
-
class Base{
-
public:
-
Base(){
-
cout<<"Base()"<<endl;
-
}
-
Base(int){
-
cout<<"Base(int)"<<endl;
-
}
-
~Base(){
-
cout<<"~Base()"<<endl;
-
}
-
Base& operator=(const Base& x){
-
cout<<"Base::=(const Base& x)"<<endl;
-
return *this;
-
}
-
Base& operator=(int x){
-
cout<<"Base::=(int x)"<<endl;
-
return *this;
-
}
-
};
-
class Derive{
-
public:
-
-
};
-
int main(){
-
Derive x;
-
Derive y(1); //error
-
Derive z;
-
x=z;
-
x=1; //error
-
return 0;
-
}
非自动继承的函数包括父类的构造函数,析构函数和复制操作符=,注意,此处虽然是非自动继承,但并不妨碍派生类自动调用父类的构造函数Base()和析构函数~Base()。另外要注意,虽然重定义一个类的构造函数之后,编译器不会再生成默认构造函数,但重定义赋值操作符且参数是其他类型时,编译器仍然回升诚意个标准的复制操作符。
阅读(254) | 评论(0) | 转发(0) |