问一个不能被自动继承的例子
时间:2011-12-13
来源:互联网
C++ 编程思想里面提到构造函数、析够函数、operator=不能被自动继承,好看个例子:
#include <iostream>
using namespace std;
class GameBoard {
public:
GameBoard()
{
cout << "GameBoard() " << endl;
}
GameBoard(const GameBoard&)
{
cout << "GameBoard(const GameBoard&)" << endl;
}
GameBoard& operator=(const GameBoard&)
{
cout << "GameBoard operator=" << endl;
return *this;
}
~GameBoard()
{
cout << "~GameBoard()" << endl;
}
};
class Game {
GameBoard gb;
public:
Game()
{
cout << "Game()" << endl;
}
Game(const Game& g) : gb(g.gb)
{
cout << "Game(const Game&)" << endl;
}
Game(int) { cout << "Game(int) " << endl;}
//显示调用
Game& operator=(const Game& g)
{
gb = g.gb;
cout << "Game::operator=" << endl;
return *this;
}
class Other{};
operator Other() const
{
cout << "Game::operator Other()" << endl;
return Other();
}
~Game(){cout << "~Game()" << endl;}
};
class Chess : public Game {};
void f(Game::Other)
{
cout << "f(Game::Other)" << endl;
}
int main()
{
Chess d1;
cout << endl;
Chess d2(d1);
cout << endl;
cout << "d1 = d2: " << endl;
d1 = d2;
}
输出:
GameBoard()
Game()
GameBoard(const GameBoard&)
Game(const Game&)
d1 = d2:
GameBoard operator=
Game::operator=
~Game()
~GameBoard()
~Game()
~GameBoard()
既然不能自动调用,为什么还调用了基类的构造函数、析够函数、operator=。。。什么意思呢这里???尤其是这个=号,既然这样子还显式调什么呢??我表示很疑问。。
#include <iostream>
using namespace std;
class GameBoard {
public:
GameBoard()
{
cout << "GameBoard() " << endl;
}
GameBoard(const GameBoard&)
{
cout << "GameBoard(const GameBoard&)" << endl;
}
GameBoard& operator=(const GameBoard&)
{
cout << "GameBoard operator=" << endl;
return *this;
}
~GameBoard()
{
cout << "~GameBoard()" << endl;
}
};
class Game {
GameBoard gb;
public:
Game()
{
cout << "Game()" << endl;
}
Game(const Game& g) : gb(g.gb)
{
cout << "Game(const Game&)" << endl;
}
Game(int) { cout << "Game(int) " << endl;}
//显示调用
Game& operator=(const Game& g)
{
gb = g.gb;
cout << "Game::operator=" << endl;
return *this;
}
class Other{};
operator Other() const
{
cout << "Game::operator Other()" << endl;
return Other();
}
~Game(){cout << "~Game()" << endl;}
};
class Chess : public Game {};
void f(Game::Other)
{
cout << "f(Game::Other)" << endl;
}
int main()
{
Chess d1;
cout << endl;
Chess d2(d1);
cout << endl;
cout << "d1 = d2: " << endl;
d1 = d2;
}
输出:
GameBoard()
Game()
GameBoard(const GameBoard&)
Game(const Game&)
d1 = d2:
GameBoard operator=
Game::operator=
~Game()
~GameBoard()
~Game()
~GameBoard()
既然不能自动调用,为什么还调用了基类的构造函数、析够函数、operator=。。。什么意思呢这里???尤其是这个=号,既然这样子还显式调什么呢??我表示很疑问。。
作者: zhjlhd 发布时间: 2011-12-13
那是因为你的类里面包含了那些类成员,肯定会调用构造函数,析构函数的嘛
作者: W170532934 发布时间: 2011-12-13
这些函数不能被自动继承的意思是:它们不会自动变成派生类的成员函数。除非你在派生类中显式定义了它们。
举个简单的例子:
class base
{
public:
base(int i): m_i(i);
base(){}
private:
int m_i;
};
class derived: public base {}
derived d(1); //编译出错,一个参数的构造函数并没有被继承,所以派生类并没有一个参数的构造函数。当然,编译器会生成一个不带参数的默认构造函数,所以 derived d;不会出错。
不能被继承并不表示不能被调用,派生类的构造函数还是可以调用基类的构造函数来完成基类部分的初始化。
举个简单的例子:
class base
{
public:
base(int i): m_i(i);
base(){}
private:
int m_i;
};
class derived: public base {}
derived d(1); //编译出错,一个参数的构造函数并没有被继承,所以派生类并没有一个参数的构造函数。当然,编译器会生成一个不带参数的默认构造函数,所以 derived d;不会出错。
不能被继承并不表示不能被调用,派生类的构造函数还是可以调用基类的构造函数来完成基类部分的初始化。
作者: tonywearme 发布时间: 2011-12-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28