在派生类对象作为参数的基类中声明纯虚函数

克里斯托夫·奥尔蒂斯(Christophe J.

抱歉,如果您之前已经提出并解决过此主题。

我想创建一个具有纯虚函数(抽象类)的基类,以便从该基类创建派生类时,用户必须在派生类中实现此函数的主体。关键是在纯虚函数中,我想将派生类型的对象作为参数对象。那可能吗 ?

例如:

class Base{

 public:
  virtual void DoSomething (A object1, B object2) = 0;
};

class A: public Base{

 public:
  DoSomething(A x, B y) {

   ...
   ...
};

};

class B: public Base{

 public:
  DoSomething(A x, B y) {

   ...
   ...
};

};

我尝试过,但是编译器抱怨说它对派生类一无所知,我理解,因为它们是在基类之后创建的。有办法规避吗?还是有另一种方法可以执行我想要的操作,即强制用户使用派生类类型的arguments arguments对象在派生类中实现函数的主体?

提前致谢 !

瓦伦丁·海尼兹(Valentin Heinitz)

编辑:使用const引用的最终版本:-)

///Test-code. place anywhere in global C++ scope for testing
class A;
class B;

class Base{

 public:
  virtual void DoSomething ( const A & object1, const B & object2) = 0;
};

class A: public Base{

 public:
  void DoSomething(const A & object1, const B & object2) { 
    int i=0;
    i++; //test break point
  };

};

class B: public Base{

 public:
  void DoSomething(const A & object1, const B & object2) { 
    int i=0;
    i++; //test break point
  };

};

bool test()
{
    A a;
    B b;

    a.DoSomething( A(), B() );
    b.DoSomething( A(), B() );
    return true;
}

static bool invokeTest = test();
///END of test-code

编辑:使用auto_ptr的更好的版本

///Test-code. place anywhere in global C++ scope for testing
#include <memory>
using std::auto_ptr;
class A;
class B;




class Base{

 public:
  virtual void DoSomething (auto_ptr<A> object1, auto_ptr<B> object2) = 0;
};

class A: public Base{

 public:
  void DoSomething(auto_ptr<A> x, auto_ptr<B> y) { 
    int i=0;
    i++; //test break point
  };

};

class B: public Base{

 public:
  void DoSomething(auto_ptr<A> x, auto_ptr<B> y) { 
    int i=0;
    i++; //test break point
  };

};

bool test()
{
    Base *a = new A;
    Base *b = new B;

    a->DoSomething( auto_ptr<A>(new A), auto_ptr<B>(new B) );
    b->DoSomething( auto_ptr<A>(new A), auto_ptr<B>(new B) );
    return true;
}

static bool invokeTest = test();
///END of test-code

原始答案

如果参数是指针,则它应该起作用:

class A;
class B;

class Base{

 public:
  virtual void DoSomething (A * object1, B * object2) = 0;
};

class A: public Base{

 public:
  void DoSomething(A *x, B *y) {

};

};

class B: public Base{

 public:
  void DoSomething(A *x, B *y) {

};

};

PS:我不知道您为什么需要它?

PPS编辑:我仍然想知道为什么会需要它:-)?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

派生类中的C ++纯虚函数实现

派生类的虚函数调用基类的虚函数

如何使用派生类中的虚函数,在另一个带有基类参数的类中声明?

具有模板化派生类的基类纯虚函数

派生类中的虚函数行为

C ++依赖于派生类的纯虚函数

通过使派生类中的纯虚拟函数“纯”虚拟化来“隐藏”基类虚函数是否有效?

为什么基类指针指向基类中的纯虚方法而不是派生类中的重写方法?

C++ 继承:基类类型的虚函数中派生类类型的参数

无法从将基类指针作为参数的函数中访问派生类成员?

当派生类中的函数无法覆盖基类中的虚函数时发出警告

模板派生类中的错误“调用了纯虚函数”

通过派生类的实例在基础中调用纯虚函数

为什么虚函数优于派生类对象?

在基类构造函数中使用抽象类作为参数,以在派生类中具有更多特定类型

如何调用在C ++中具有抽象基类和派生类的定义的基类的虚函数定义?

派生类不使用来自基类的所有纯虚方法

为什么派生类中的虚函数也虚拟?

访问虚函数中的派生类成员变量

如何覆盖派生类中的虚函数?

如何在派生类中实现虚函数?

在派生类中调用多个虚函数

(为什么)在纯虚拟派生类中是否需要虚拟基类构造函数调用?

基类可以声明一个虚方法而不定义它吗?仍然在派生类中定义

在声明派生类时调用基类的构造函数

如果基类作为参数调用,我可以使用派生类函数吗?

Cython / Python / C ++-继承:将派生类作为参数传递给需要基类的函数

派生类函数参数作为基类引用导致C2678

重载函数,以便它可以将基类转换为派生类作为参数