构造与析构函数以及=操作符不能被继承,以及内部类的用法
2014-04-27 22:27
309 查看
不是所有的函数都能自动地从基类继承到派生类中的。构造函数和析构函数是用来处理对象的创建和析构的,它们只知道对在它们的特殊层次的对象做什么。所以,在整个层次中的所有的构造函数和析构函数都必须被调用,也就是说,构造函数和析构函数不能被继承。
另外,operator= 也不能被继承,因为它完成类似于构造函数的活动。
输出结果:
root()
root(root&)
root::operator=()
root::operator other()
~root()
~root()
另外,operator= 也不能被继承,因为它完成类似于构造函数的活动。
//: NINHERIT.CPP -- Non-inherited functions #include <fstream.h> class root { public: root() { cout << "root()\n"; } root(root&) { cout << "root(root&)\n"; } root(int) { cout << "root(int)\n"; } root& operator=(const root&) { cout << "root::operator=()\n"; return *this; } class other {}; // 定义内部类 operator other() const { cout << "root::operator other()\n"; return other(); } ~root() { cout << "~root()\n"; } }; class derived : public root {}; void f(root::other) {} // 使用内部类,这样定义不多见 main() { derived d1; // Default constructor derived d2 = d1; // Copy-constructor //! derived d3(1); // Error: no int constructor d1 = d2; // Operator= not inherited f(d1); // Type-conversion IS inherited }
输出结果:
root()
root(root&)
root::operator=()
root::operator other()
~root()
~root()
相关文章推荐
- c++, 派生类的构造函数和析构函数 , [ 以及operator=不能被继承 or Not的探讨]
- C++继承时构造与析构函数以及using,继承时的遮蔽浅谈
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
- 黑马程序员—继承多态以及内部类
- 一道关于C++ 的面试题目(继承、构造函数以及析构函数)
- 黑马程序员—继承多态以及内部类
- 编写类String的构造/析构函数,赋值操作符
- android中SimpleAdapter、ArrayAdapter的用法以及继承了ListActivity的设置适配器的方法
- java中this用法 super()用法 以及构造方法
- Java第11次作业:什么是继承?继承的好处?什么是覆写?super()?构造代码块?子父类初始化顺序? 抽象类能用final声明吗?final关键字声明类 方法 变量以及全局常量?抽象类的构造方法?
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容 【转】 参考度4.6星
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容 【转】 参考度4.6星
- c++ 继承 + 构造、析构函数
- C++继承中析构函数 构造函数的调用顺序以及虚析构函数
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
- C++继承中析构函数 构造函数的调用顺序以及虚析构函数
- 继承中,构造函数,赋值操作符,析构函数与虚函数的关系
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
- JAVA继承,构造函数的执行以及super(),super的用法
- virtual与构造/析构函数(内联函数,构造函数,静态成员函数不能为virtual函数)