c++ - template operator overload base class -



c++ - template operator overload base class -

i have base of operations class many kid classes. how implement template operator on loader in base of operations class work inheriting classes? tried create 1 + operator complained had many parameters. i'm not sure right way go doing (i'm starting utilize oop) if can think of improve way great.

i'm making library in each metric space class. want create base of operations class "operations" every space inherits.

my template base of operations class:

#ifndef __libspace__operations__ #define __libspace__operations__ template< typename t > class operations{ public: friend t operator+( const t& sp, const t& nsp ){ homecoming 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; homecoming 0; }

a fellow member operator +() have 1 parameter, right operand. left or first *this. depending of situation, need base of operations +, virtual + or template. free operator +() take 2 argumets, "left" , "right".

in code:

template< typename t > class operations{ public: friend t operator+( const t& sp, const t& nsp ){ homecoming t(sp.dimension + nsp.dimension); }; };

you whant fellow member or friend?

if friend, problems +() have define outside class, friend, not member.

template< typename t > t operator+( const t& sp, const t& nsp ); template< typename t > class operations{ public: friend t operator+<t>( const t& sp, const t& nsp ); }; template< typename t > t operator+( const t& sp, const t& nsp ) { homecoming t(sp.dimension + nsp.dimension); }

but !!!! have real problem: +() uses privat fellow member of derived class, not base of operations class, need friend of derived class. think need rethink ;-) design. if confortable using dimension in operations.. protected fellow member of operations??? operations have dimension?

c++ oop templates operator-overloading base-class

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -