LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1071|回复: 4

C++中类型向上转化的问题

[复制链接]
发表于 2006-10-4 22:31:34 | 显示全部楼层 |阅读模式
各位LinuxSir的大侠们,您们好,小弟实做如下代码时遇到了问题:

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

  4. class Fruit
  5. {
  6. public:
  7.         Fruit(string name):m_sName(name){}
  8.         virtual void eat(){}
  9. protected:
  10.         string m_sName;
  11. };

  12. class Apple: public Fruit
  13. {
  14. public:
  15.         void eat(){cout << m_sName << "削皮吃" << endl;}
  16. };

  17. class Banana: public Fruit
  18. {
  19. public:
  20.         void eat(){cout << m_sName << "剥皮吃" << endl;}
  21. };

  22. int main(void)
  23. {
  24.         Fruit *pFruit;
  25.        
  26.         pFruit = new Apple("红苹果");
  27.         pFruit->eat();

  28.         pFruit = new Banana("进口香蕉");
  29.         pFruit->eat();

  30.         pFruit = NULL;
  31.         return 1;
  32. }
复制代码

在Debian下g++(3.3.5)编译,error如下:

  1. 1.cpp:15: error: base `Fruit' with only non-default constructor in class without a constructor
  2. 1.cpp:21: error: base `Fruit' with only non-default constructor in class without a constructor
  3. 1.cpp: In function `int main()':
  4. 1.cpp:30: error: no matching function for call to `Apple::Apple(const char[7])'
  5. 1.cpp:15: error: candidates are: Apple::Apple(const Apple&)
  6. 1.cpp:33: error: no matching function for call to `Banana::Banana(const char[9])'
  7. 1.cpp:21: error: candidates are: Banana::Banana(const Banana&)
复制代码

小弟的疑问是:在Fruit中已经有了构造函数,而Apple和Banana是其派生类,也需要吗?还有该pFruit做转化类型时为何要求匹配的Apple&或Banana&?
还请各位老大指点下该如何修改及为什么,谢谢了^_^
发表于 2006-10-4 23:23:19 | 显示全部楼层
构造函数和复制控制成员是不能继承的,如果派生类不定义这些,就会使用默认的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-5 00:39:48 | 显示全部楼层
Post by zlbruce
构造函数和复制控制成员是不能继承的,如果派生类不定义这些,就会使用默认的

这里的“默认”是指基类的构造函数吗?例如:

  1. class A
  2. {
  3. public:
  4.         A() { cout << "I'm A class" << endl; }
  5. };
  6. class B : public A
  7. {
  8. public:
  9.         B() { cout << "I'm B class" << endl; }
  10. };
  11. ...
  12. B b;
复制代码

这时会依次出现I'm A class和I'm B class。现在为Apple等派生类添加上构造函数后,编译时仍然告诉没有和Fruit匹配。请老大们明确指出该如何修改code好吗?
回复 支持 反对

使用道具 举报

发表于 2006-10-5 00:53:32 | 显示全部楼层
我说的默认是如果你不给一个类提供构造函数,那么 C++ 会自动提供一个。
修改这个程序,只需在派生类里面添加一个构造函数即可,请注意 new 的对象需要用 delete 来释放。
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Fruit
  5. {
  6. public:
  7.         Fruit(string name):m_sName(name){}
  8.         virtual void eat(){}
  9. protected:
  10.         string m_sName;
  11. };
  12. class Apple: public Fruit
  13. {
  14. public:
  15.         Apple(string name):Fruit(name){}
  16.         void eat(){cout << m_sName << "削皮吃" << endl;}
  17. };
  18. class Banana: public Fruit
  19. {
  20. public:
  21.         Banana(string name):Fruit(name){}
  22.         void eat(){cout << m_sName << "剥皮吃" << endl;}
  23. };
  24. int main(void)
  25. {
  26.         Fruit *pFruit;
  27.        
  28.         pFruit = new Apple("红苹果");
  29.         pFruit->eat();
  30.         [color=red]delete pFruit;[/color]
  31.         pFruit = new Banana("进口香蕉");
  32.         pFruit->eat();
  33.         [color=red]delete pFruit;[/color]
  34.         pFruit = NULL;
  35.         return 1;
  36. }
复制代码
至于你说的会依次出现I'm A class和I'm B class,是因为在构造派生类时要先构造基类
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-5 10:55:04 | 显示全部楼层
谢谢指导^_^
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表