M
Magnus Jonneryd
I want to be able to define a super class that defines some basic functionality
and also some helpers that define, mainly, arithmetic operations, but I've been
having some trouble.
The following example illustrates what i want to do:
template<typename T = int>
class A{};
template<typename T = int>
class B: public A<T>{};
template<typename T>
A<T> operator*(const A<T>& lhs, const A<T>& rhs){
return lhs;
}
int main(){
B<> b_1;
B<> b_2;
B<> b_3;
b_3 = b_1*b_2;
}
But when I compile it I get the following:
tst.cc: In function `int main()':
tst.cc:16: error: no match for 'operator=' in 'b_3 = operator* [with T
=int](((const A<int>&)((const A<int>*)((A<int>*)(&b_1)))), ((const A<int>&)
((const A<int>*)((A<int>*)(&b_2)))))'
tst.cc:5: note: candidates are: B<int>& B<int>:perator=(const B<int>&)
Which I think means that the assignment operator ain't defined so that it's possible
to assign an instance of class A to class B. I was hoping that the inheritance would
make this possible, but alas.
What i then did was to define the operator as:
template<typename T>
T operator*(const T& lhs, const T& rhs){
return lhs;
}
Which works, but won't this cause a problem if i try multiplying something else
since this is quite a general solution? What I wanted to do was to be able to just
add some functionality in my sub-classes and be able to use the helpers that i
defined in the super class, and be able to use the same scheme with another
set of classes (i.e D: public D). But if i do that won't that mean that the two
functions will "collide"?
Thanks in advance.
and also some helpers that define, mainly, arithmetic operations, but I've been
having some trouble.
The following example illustrates what i want to do:
template<typename T = int>
class A{};
template<typename T = int>
class B: public A<T>{};
template<typename T>
A<T> operator*(const A<T>& lhs, const A<T>& rhs){
return lhs;
}
int main(){
B<> b_1;
B<> b_2;
B<> b_3;
b_3 = b_1*b_2;
}
But when I compile it I get the following:
tst.cc: In function `int main()':
tst.cc:16: error: no match for 'operator=' in 'b_3 = operator* [with T
=int](((const A<int>&)((const A<int>*)((A<int>*)(&b_1)))), ((const A<int>&)
((const A<int>*)((A<int>*)(&b_2)))))'
tst.cc:5: note: candidates are: B<int>& B<int>:perator=(const B<int>&)
Which I think means that the assignment operator ain't defined so that it's possible
to assign an instance of class A to class B. I was hoping that the inheritance would
make this possible, but alas.
What i then did was to define the operator as:
template<typename T>
T operator*(const T& lhs, const T& rhs){
return lhs;
}
Which works, but won't this cause a problem if i try multiplying something else
since this is quite a general solution? What I wanted to do was to be able to just
add some functionality in my sub-classes and be able to use the helpers that i
defined in the super class, and be able to use the same scheme with another
set of classes (i.e D: public D). But if i do that won't that mean that the two
functions will "collide"?
Thanks in advance.