覆盖C++虚函数时出错

din*_*hil 3 c++ inheritance binding subclass

这是我的情况:

class Filter3by3 {
public:
   virtual inline Mat convolution((Mat & mat, int i, int j, int rows, int cols) { 
 code

   }
};

class MySobel: public Filter3by3 {
public:
  inline Vec3b convolution(Mat & mat, int i, int j, int rows, int cols) {
    code
  }
};
Run Code Online (Sandbox Code Playgroud)

现在,当我打电话:

Filter3by3 f = choose_filter(filtername); // Returns a Sobel filter
Mat mat;
s.convolution(args);
Run Code Online (Sandbox Code Playgroud)

调用基类方法.我是c ++方法绑定规则的新手,所以你能告诉我哪里错了吗?我感谢您的帮助.

更新 似乎即使使用虚拟内联Mat卷积((Mat&mat,int i,int j,int rows,int cols)它也不起作用.

这是一个正在运行的程序,用g ++ -std = c ++ 11编译

#include <iostream>

using namespace std;

class Filter {
public:
  Filter() { }
  virtual int ehi() {
    cout << "1" << endl;
    return 1;
  }

};

class SubFilter : public Filter {
public:
  SubFilter() : Filter() { }

  int ehi() {
    cout << "2" << endl;
    return 2;
  }

};

  Filter choose_filter(){
    SubFilter f;
    return f;
  }

  int main(int argc, char* argv[]) {

     Filter f = choose_filter();
     f.ehi();
     return 0;
  }
Run Code Online (Sandbox Code Playgroud)

它打印1而不是2.我使用虚拟来确保动态绑定,但它似乎不够,还有"覆盖"关键字.

The*_*hel 6

重写方法必须具有相同的签名,即参数和返回类型,作为基本方法.如果将override关键字添加到签名中,编译器可以通知您这些是否匹配.