Arithmetic Operator Overload in Pure Abstract Class
-
This is a tough one, I want to overload the arithmetic operators in a pur virtual abstract class.:confused: The pur abstract class defines the basic 'Interface' from there I have a base parent class that will be the default implementation and several parallel child classes. e.g.
virtual IClass operator+(IClass& RHS) = 0;
Then I can implement it in BaseClass. Problem: Can not instantiate abstract class. changing it to
virtual IClass& operator+(IClass& RHS) = 0;
means that the statement is valid BUT it can't work (or can it?) this returns a reference to IClass which isn't what we want in addition, we want to return a value not a reference. So how can I do this whithou getting in to memory leaks and such. For addition we want to be able to do result = a + b + c + d, etc. This is pure old (not C++11) C++ I'm working in today.
CJ
-
This is a tough one, I want to overload the arithmetic operators in a pur virtual abstract class.:confused: The pur abstract class defines the basic 'Interface' from there I have a base parent class that will be the default implementation and several parallel child classes. e.g.
virtual IClass operator+(IClass& RHS) = 0;
Then I can implement it in BaseClass. Problem: Can not instantiate abstract class. changing it to
virtual IClass& operator+(IClass& RHS) = 0;
means that the statement is valid BUT it can't work (or can it?) this returns a reference to IClass which isn't what we want in addition, we want to return a value not a reference. So how can I do this whithou getting in to memory leaks and such. For addition we want to be able to do result = a + b + c + d, etc. This is pure old (not C++11) C++ I'm working in today.
CJ
-
This is a tough one, I want to overload the arithmetic operators in a pur virtual abstract class.:confused: The pur abstract class defines the basic 'Interface' from there I have a base parent class that will be the default implementation and several parallel child classes. e.g.
virtual IClass operator+(IClass& RHS) = 0;
Then I can implement it in BaseClass. Problem: Can not instantiate abstract class. changing it to
virtual IClass& operator+(IClass& RHS) = 0;
means that the statement is valid BUT it can't work (or can it?) this returns a reference to IClass which isn't what we want in addition, we want to return a value not a reference. So how can I do this whithou getting in to memory leaks and such. For addition we want to be able to do result = a + b + c + d, etc. This is pure old (not C++11) C++ I'm working in today.
CJ
One way to do it is by adding (pure) virtual accessors that read the elements that you need for the addition, and (pure) virtual factory methods that you can use to create an object of the correct type. The following might work:
class vecbase {
public:
virtual int size() const = 0;
virtual int& operator[](int i) = 0;
virtual int operator[](int i) const = 0;
virtual vecbase create(int sz, int* values) = 0;friend vecbase operator+(const vecbase& a, const vecbase& b) {
assert(a.size() == b.size());
int* values = new int[a.size()];
for (int i = 0; i < a.size(); ++i) {
values[i] = a[i] + b[i];
}
result = create(a.size(), values);
}
};Note that I declared the operator as a friend function, rather than a member function. I prefer that approach for binary operators in case I need operators for mixed argument types. The functions in the derived classes should then change the result types of the overriden functions to the appropriate type:
class vec2 : public vecbase {
private:
int val[2]
public:
vec2(int* values) {
assert(values != nullptr);
val[0] = values[0];
val[1] = values[1];
}
int size() const { return 2; }
int& operator[](int i) { return val[i]; }
int operator[](int i) const { return val[i]; }
// change return type on this override - it is tstill considered an override!
vec2 create(int sz, int* values) {
assert(sz==2);
return vec2(values);
}
};I haven't tested this or even compiled, but this should be good enough to get the idea. P.S.: just realized that I forgot to release the values array - that one will be a bit tricky, maybe you need additional pure virtual helpers to create and release a sufficiently large initialization array... P.P.S.: I just realized this could be done much easier - all you need is a pure virtual function that performs the addition, and creates the resulting object as well:
class vecbase {
...
virtual vecbase add(const vecbase&, const vecbase&) = 0;friend vecbase operator+(const vecbase& a, const vecbase& b) {
return add(a, b);
}
};
class vec2 : public vecbase {
...
public:
vec2 add(const vecbase&, const vecbase&) { ... }
};Of course, that defeats the purpose of implementing the operator in the base class - but then you can't really do that without virtual helper functions anyway.
GOTOs are a bit like wire