C++ 继承 + 虚函数

Mr_Alvin 2013-06-15 12:20:26
#include<iostream>
#include<string>
using namespace std;

class Platform {
public:
virtual string sysinfo() = 0;
virtual ~Platform() {}
};
class MacOS : public Platform{
public:
string sysinfo() {
return "MacOS owo";
}
~Platform() {}
};
class Windows : public Platform{
public:
string sysinfo() {
return "Windows T_T";
}
~Platform() {}
};

编译错误...
为什么积累中的虚函数 ~Platform() {} 不能写在子类中呢?
...全文
137 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohutushen30 2013-06-16
  • 打赏
  • 举报
回复
引用 3 楼 alipu07 的回复:
[quote=引用 1 楼 FancyMouse 的回复:] 得写 ~MacOS() ~Windows() 没为什么。
但他那个是继承上面的啊? 那个析构函数不能那样继承吗?能讲解的详细点吗?[/quote] 析构函数是一个特例的函数,析构函数和构造函数是C++中的两个怪物,无论从他的写法还是使用都不能用一般的思维去使用!
Mr_Alvin 2013-06-15
  • 打赏
  • 举报
回复
引用 1 楼 FancyMouse 的回复:
得写 ~MacOS() ~Windows() 没为什么。
但他那个是继承上面的啊? 那个析构函数不能那样继承吗?能讲解的详细点吗?
Mr_Alvin 2013-06-15
  • 打赏
  • 举报
回复
还有一个问题

class Game {
  protected:
     Platform *platform_;
  public:
 //        Game() {}          // 这句没有的话不能编译啊
     virtual string play() = 0;
    Game(Platform *backend) { platform_ = backend;}
    virtual ~Game() {
            delete platform_;
            }
};
class Diablo : public Game {
 public:
      Diablo(Platform *backend)  { platform_ = backend; }
        string play(){
               string str = "Playing Diablo on ";
               str = str + platform_->sysinfo();
               return str;
               }
        virtual ~Diablo() {delete platform_;}
};

class Warcraft : public Game{
 public:
      Warcraft(Platform *backend)  { platform_ = backend; }
        string play(){
               string str = "Playing Warcraft on ";
               str = str + platform_->sysinfo();
               return str;
               }
        virtual ~Warcraft() {delete platform_;}
};
为什么基类中没有 Game() { } 的话 ,不能编译啊?
FancyMouse 2013-06-15
  • 打赏
  • 举报
回复
得写 ~MacOS() ~Windows() 没为什么。

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧