基方法/运算符在派生 (C++) 上调用时返回基类型
Base methods/operators return base type when called on derived (C++)?
给定:
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
- 如何安全地从 DLL 调用返回对象
- 类对象属性是否可以调用返回属性应具有的值的方法C++?
- 从类似构造函数的调用返回模板化智能指针
- C ++ DLL导入:函数调用返回错误的结果
- 如何调用返回类方法指针的类方法
- 如何调用返回双精度或字符串并将其保存在地图上但作为对象的类的方法
- FindResource 调用返回 RCDATA 类型的文本文件的 NULL
- 如何调用返回指针数组的函数?C++
- Perl xs - 无法从方法调用返回新的自定义 C++ 对象 - 改为返回标量值
- BOOST ::线程内会成员功能调用返回unique_lock Instantiation错误
- 断言调用返回的函数的签名 &(引用)
- 对于相同的输入参数,多个函数调用返回不同的结果
- 如何调用返回后删除[]内存
- 使用二维数组调用返回结构
- C++:声明、定义和调用返回模板类对象的函数
- 调用返回数组的函数
- c++方法从c#dllimport调用返回0
- 方法调用返回空值
- 我希望计时器只在上一个调用返回或完成执行时调用代码
- 任何时间函数保证为调用返回不同的值