简体   繁体   English

C ++:如何在此抽象类的方法中返回抽象类

[英]C++: How to return abstract class in a method of this abstract class

I have an abstract class and it has two derived classes. 我有一个抽象类,它有两个派生类。 I'm trying to overload operator+ in the base class so I could do derivedClass=derivedClass+differentDerivedClass like this: 我正在尝试重载基类中的operator +,所以我可以像这样执行derivedClass = derivedClass + differentDerivedClass:

class iMatrix{
iMatrix operator+(const iMatrix& obj)
}

class UsualMatrix : public iMatrix

class SparseMatrix : public iMatrix 

Inside of main, I want to do 在主要内部,我想做

SparseMatrix a, b;
UsualMatrix c=a+b;

I have copy constructors and everything that may be helpful. 我有复制构造函数和可能有用的一切。 Now if I create the function like 现在如果我创建像这样的函数

iMatrix operator+(const iMatrix& obj)

Compiler (LLVM in Xcode) says "Return type 'iMatrix' is an abstract class", but theoretically everything else should work properly. 编译器(Xcode中的LLVM)说“返回类型'iMatrix'是一个抽象类”,但理论上其他一切都应该正常工作。 I've read manuals and other StackOverflow threads, so I've tried this: 我已经阅读了手册和其他StackOverflow线程,所以我试过这个:

iMatrix& operator+(const iMatrix& obj)

Whereas function on its own does "add obj to this" and returns *this. 而函数本身就“添加obj到此”并返回* this。 The problem is when I do c=a+b, a becomes equal to c, but it should not be changed. 问题是当我做c = a + b时,a变得等于c,但不应该改变。

a=
1 1 
1 1 
b=
1 1 
1 1 
a+b=c; 
c=
2 2 
2 2 
b=
1 1 
1 1 
a=
2 2 
2 2 

What should I do? 我该怎么办? Addition and multiplying must be implemented inside of base class, and I have no idea how to fix this error. 必须在基类内部实现加法和乘法,我不知道如何修复此错误。 Any advice would be appreciated, thanks. 任何建议将不胜感激,谢谢。

The problem with your intend design is the abstract return type. 您的意图设计的问题是抽象返回类型。 Abstract classes can't be returned by value. 无法通过值返回抽象类。 Only concrete classes can be returned by value. 只能通过值返回具体类。 So, to return abstract class you'd need a pointer or a reference. 因此,要返回抽象类,您需要一个指针或引用。

Unfortunately, this will not work with the semantics of operator+() : 不幸的是,这不适用于operator+()的语义:

  • either you'd return by reference the current object, and change it with the operation; 要么通过引用返回当前对象,要么通过操作进行更改; but this is not compliant with the expected semantics, and furthermore might lead to wrong answerds (eg when doing c = a+b+a+b; ) 但这不符合预期的语义,而且可能导致错误的答案(例如,当做c = a+b+a+b; )时
  • or you'd return the reference to a temporary object. 或者您将返回对临时对象的引用。 but this would be UB, because the temporaray object will decease when the function returns, making the reference irrelevant. 但这将是UB,因为temporaray对象将在函数返回时降低,使得引用无关紧要。
  • finally operator+ would not have a way to decide on its own of the concrete type to use for the return. 最后, operator+无法自行决定用于返回的具体类型。 For example: SparseMatrix a; UsualMatrix b; a+b; 例如: SparseMatrix a; UsualMatrix b; a+b; SparseMatrix a; UsualMatrix b; a+b; what should be the type to be used for the matrix a+b ? 什么应该是矩阵a + b使用的类型?

You can use polymorphic and abstract types within operators, but not as a return type. 您可以在运算符中使用多态和抽象类型,但不能作为返回类型。 To implement this kind of things, you'd need to opt for your own set of operators, and letting the caller manage the temporary results manually: 要实现此类操作,您需要选择自己的一组操作符,并让调用者手动管理临时结果:

UsualMatrix c,tmp; 
SparseMatrix a,b,x; 
...
a.add(b, tmp);
tmp.add(x, c);   // c = a+b+x; 

with virtual void add(const iMatrix& x, iMatrix z) = 0; with virtual void add(const iMatrix& x, iMatrix z) = 0;

One solution could be a non-member function like this: 一个解决方案可能是这样的非成员函数:

template <
  typename T,
  typename = typename std::enable_if<std::is_base_of<iMatrix, T>::value>::type
>
T operator+(T const & a, T const & b) {
   T res; 
   // calculate res
   return res;
}

Because iMatrix is an abstract class, you can not return an instance of it. 因为iMatrix是一个抽象类,所以不能返回它的实例。 In the example above we are overloading operator+ for all derived classes T of iMatrix and we can return T instances. 在上面的例子中,我们为iMatrix所有派生类T重载operator+ ,我们可以返回T实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM