基方法/运算符在派生 (C++) 上调用时返回基类型

Base methods/operators return base type when called on derived (C++)?

本文关键字:调用 返回 类型 基类 C++ 方法 运算符 派生      更新时间:2023-10-16

给定:

class Base:
{
  public:
  ...
  Base operator+( const Base& other );
  Base& scale( float num );
}
class Derived : public Base:
{
  public:
  ...
  Derived( const Base& other );
  float super_math_wizardry();
}
//A user can do the following:
Base b1,b2,b3;
Derived d1,d2,d3;
b3 = b1 + b2;
d3 = d1 + d2;
b3 = b1.scale( 2.0f );
d3 = d1.scale( 3.0f ); //scale returns Base& type that is converted to Derived
float x,y;
x = (d1+d2).super_math_wizardry(); //compiler type error since d1+d2 returns Base type
y = (d1.scale(4.0f)).super_math_wizardry(); //similar compiler error
x = Derived(d1+d2).super_math_wizardry(); //works
y = Derived(d1.scale(4.0f)).super_math_wizardry(); //works

有没有办法使前两个语句工作,而无需重新实现派生中的每个 Base 方法(使派生方法调用 Base 方法并返回派生类型),并且不需要用户进行强制转换/调用复制构造函数?

编辑:所有派生对象都在 Base 对象集中(根据类继承的需要),但并非所有 Base 对象都在派生对象集中。 它们具有相同的数据成员,但派生对象具有分配给其中一个数据成员的常量值(所有派生对象的常量值相同)。

有许多特定于 Base 或 Derived 的方法,但大多数运算符和 set/get 访问器在 Base 和 Derived 对象上具有相同的定义行为。当我在派生对象上调用 Base 方法时,我正在尝试做的事情是获取 Derived 或 Derived&(因为这些操作在数学上定义为这样做),同时在 Base 对象上调用 Base 方法时仍然获取 Base 或 Base&。

上下文:Base 是一个矩阵类,Derived 是一个向量(列)类。 Derived(const Base&other )构造函数用于从单列(nx1)矩阵中显式获取Vector。

所以我想要:

x = (d1+d2).super_math_wizardry(); //works
y = (b1+b2).super_math_wizardry(); //fails (although possibly at run-time since a nx1 Matrix is a column vector)

鉴于您的上下文,我认为您遇到的基本问题是通知编译器Derived对象集在 operator+ 下关闭。我知道,你知道,但是C++语言中没有特殊的捷径来表达它。您确实需要实现Derived Derived::operator+(const Derived&) const

我可能会explicit Derived(const Base &other)构造函数。如果other的维度错误,据推测它会引发异常,因此用户不应该期望隐式发生。他们需要知道这是对的,所以他们不妨说他们希望它发生。

简短的回答是,不。

这些函数的返回类型为 Base。您要求编译器执行的操作与执行的操作没有什么不同

Derived d1;
Base* b = &d1;
b->super_math_wizardry(); // This is also wrong since we don't know that b can be
                          // a derived class

根本没有办法做到这一点,因为语言没有办法知道上述和

Base* b1 = new Base();
b1->super_math_wizardry(); // This is just plain wrong