I have a base class with many child classes. How would I implement a template operator over loader in the base class to work for all the inheriting classes? I tried to make one with the + operator but it complained that I had too many parameters. I'm not actually sure this is the right way to go about doing this (I'm just starting to use OOP) so if you can think of a better way that would also be great.
I'm making a library in which each metric space is a class. I want to make a base class "Operations" that every space inherits.
My template base class:
#ifndef __libSpace__Operations__
#define __libSpace__Operations__
template< typename T >
class Operations{
public:
friend T operator+( const T& sp, const T& nsp ){
return T(sp.dimension + nsp.dimension);
};
};
#endif
child:
#ifndef __libSpace__EuclidSP__
#define __libSpace__EuclidSP__
#include "Operations.h"
class EuclidSP: public Operations<EuclidSP>{
public:
EuclidSP(int n = 0, ...);
~EuclidSP();
double* vector();
private:
int dimension;
double *vec = new double(dimension);
};
#endif
main:
#include <iostream>
#include "EuclidSP.h"
int main(int argc, const char * argv[])
{
EuclidSP ob1(3,4.0,5.0,6.0);
EuclidSP ob2(3,2.0,5.0,3.0);
EuclidSP obj3();
obj3 = ob2+ob1;
return 0;
}