覆盖抽象基类中的函数

时间:2013-03-07 11:11:39

标签: c++ inheritance override

我有一个基类,它包含两个同名的函数,一个作为参数vector<int>,另一个int。这个想法是子类将定义自己的方法来处理int,基类将能够处理任一选项。但它目前还没有编译。

class base {
public:
  virtual void toBeCalled(int i) const = 0;

  virtual void toBeCalled(std::vector<int> iVec) const
  {
    std::cout << "base::toBeCalled(vec<int>)" << std::endl;
  }
};  

class derived : public base {
public:
  virtual void toBeCalled(int i) const
  {
    std::cout << "derived::toBeCalled(int)" << std::endl;
  }
};

int main(int argc, char* argv[])
{
  derived d;
  std::vector<int> iVec;

  int i = 0;

  d.toBeCalled (i);
  d.toBeCalled (iVec);  //<< Compile error: Cannot convert from std::vector<int> to int

  return 0;
}

我可以通过在main()base *d = new derived()中使用基类对象来实现它,但是如果可能的话我宁愿不这样做,因为我想访问一些派生类函数。

4 个答案:

答案 0 :(得分:5)

using base::toBeCalled;放入class derived

我很确定这是一个骗局,但我找不到,所以我不会详细说明。但是当你在派生类中重写(或重载)一个函数时,你会隐藏该函数的所有基类版本。

此处讨论了行为的基本原理:https://stackoverflow.com/a/12036004/13005

答案 1 :(得分:3)

您尚未导致class derived从基础派生。

class derived: public base {

答案 2 :(得分:2)

我假设您在此处复制代码时忘记指定基类。这是,而不是:

class derived {
public: // and so on
你有:

class derived : public base {
public: // and so on

为类定义方法时,这些方法会隐藏基类中具有相同名称的所有方法。在这种情况下,当您覆盖方法toBeCalled()时,您说类derived将只包含您指定的签名;基类中的任何其他签名都将被隐藏:

class derived : public base {
public:
  virtual void toBeCalled(int i) const
  {
    std::cout << "derived::toBeCalled(int)" << std::endl;
  }
  // void base::toBeCalled(std::vector<int> iVec) const is hidden and
  // cannot be accessed through this class
};

由于您不希望在派生类中覆盖方法的第二个版本,因此最好的方法是明确告诉编译器您要导入(而不是隐藏)方法的所有基类版本进入你的派生类。这是通过using子句完成的:

class derived : public base {
public:
  using base::toBeCalled; // Any version of method toBeCalled() not overriden here
                          // will now be taken from the base class
  virtual void toBeCalled(int i) const
  {
    std::cout << "derived::toBeCalled(int)" << std::endl;
  }
  // void base::toBeCalled(std::vector<int> iVec) const is taken
  // from the base class now
};

答案 3 :(得分:0)

你可以通过类似的东西来调用它,

  base.toBeCalled(iVec);  // here base is not base class name...it is reference to Parent Class 

其他方式 -

class derived : base  {  // inherit base class
public:
  virtual void toBeCalled(int i) const
  {
    std::cout << "derived::toBeCalled(int)" << std::endl;
  }
};