假设:
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
有没有办法让前两个语句工作而不重新实现Derived中的每个Base方法(make Derived方法调用Base方法并返回Derived类型)并且不需要用户进行强制转换/调用复制构造函数? / p>
编辑:所有派生对象都在Base对象集中(如类继承所需),但并非所有Base对象都在Derived对象集中。它们具有相同的数据成员,但Derived对象具有分配给其中一个数据成员的常量值(所有派生对象的常量值相同)。
有许多特定于Base或Derived的方法,但大多数运算符和set / get访问器在Base和Derived对象上具有相同的已定义行为。我要做的就是获得Derived或Derived&当我在Derived对象上调用Base方法时(因为这些操作在数学上被定义为这样做),同时仍然获得Base或Base&在Base对象上调用Base方法时。
背景:Base是Matrix类,Derived是Vector(列)类。 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)
答案 0 :(得分:3)
根据您的上下文,我认为您遇到的根本问题是通知编译器Derived
对象集在operator+
下关闭。我知道,你知道,但C ++语言中没有特殊的快捷方式来表达它。您需要实施Derived Derived::operator+(const Derived&) const
。
我可能会制作Derived(const Base &other)
构造函数explicit
。如果other
的维度错误,可能会抛出异常,因此用户不应该隐含地发生这种情况。他们需要知道这是正确的,所以他们不得不说他们希望它发生。
答案 1 :(得分:2)
简短的回答是,不。
函数的返回类型为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