CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Location
    Morelia, Mexico
    Posts
    40

    template help, operators overload and type of

    So basically I want to do the following operations:

    CMatriz<sometype> matrix1;
    sometype tg1;

    matrix1+tg1;
    matrix1+5.0;

    However:
    Code:
    	// Sobrecarga de operador + 
    	CMatriz<TG> operator+(const CMatriz<TG> &op); // CMatriz + CMatriz
    
    	CMatriz<TG> operator+(const TG &op); // Matriz + objeto TG
    	friend CMatriz<TG> operator+(const TG &op1,const CMatriz<TG> &op2); // objeto TG + CMatriz
    
    	CMatriz<TG> operator+(const double &op); // CMatriz + constante
    	friend CMatriz<TG> operator+(const double &op1,const CMatriz<TG> &op2); // constante + CMatriz
    3rd and 4th overloads conflict with 5th and 6th when TG is double. (they become the same)

    What to do?


    Thanks in advance.
    Perhaps you've seen the other thread, it seems that today I'm Mr. Question Man
    int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
    o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

  2. #2
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: template help, operators overload and type of

    Hope that helps.

    Code:
    #include <iostream>
    #include <typeinfo>
     
    template <typename TG>
    class CMatriz{
    
    public:
     
     CMatriz<TG>&  operator+(const CMatriz<TG> &op){ // CMatriz + CMatriz
       std::cout << "1. type=<" <<  typeid(TG).name() << ">" << std::endl;
       return *this;
     }
     
     template <typename TG2>
     CMatriz<TG>&  operator+(const TG2 &op){         // CMatriz + objeto TG 
       std::cout << "2. type=<" << typeid(TG).name() << ">" << std::endl;
       return *this;
     }
     
     CMatriz<TG>&  operator+(const double &op){      // CMatriz + double    
       std::cout << "3. overload double" << std::endl;
       return *this;
     }
     
     friend CMatriz<TG> operator+(const TG &op1,const CMatriz<TG> &op2){ // objeto TG + CMatriz<TG>       std::cout << "4. friend,type=" << typeid(op1).name()<< ",CMatriz<" << typeid(TG).name() <<  ">" << std::endl;
       return op2;
     }
     
     template <typename OBJ>  ){//other obj than CMatriz's + CMatriz 
     friend CMatriz<TG> operator+(const OBJ &op1,const CMatriz<TG> &op2
       std::cout << "5.  <OBJ> = <" << typeid(OBJ).name() << ",CMatriz<" << typeid(TG).name() <<  ">> " << std::endl;
       return op2;
     }
    };
    
    int main(){
     
      CMatriz<int> a;
      std::cout << " operator+ of CMatriz<int> : " << std::endl << std::endl;
      a+a;
      a+1;
      1+a;
      a+2.0;
      3.0+a;
      std::cout << std::endl << " operator+ of CMatriz<double> : " << std::endl << std::endl;
      CMatriz<double> aa;
      aa+aa;
      aa+1;
      1+aa;
      aa+2.0;
      2.0+aa;
     
      system("PAUSE"); 
      return 0;
    }
    **** **** **** **** **/**

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: template help, operators overload and type of

    I would implement operator+= and then do operator+ in terms of +=, as operator+ is hard to implement correctly as a class member. For example, it is a const method, producing a new matrix, not returning the current one. Thus return *this is wrong.

    Sometimes you can create a special class to optimise a chain of plusses that simply creates a list of the items being added, and an implicit constructor from that list. For example, let's say we have a class MatrixList (internally contains a list of pointers to const matrices).

    Then you can define:
    Code:
    MatrixList operator+( const Matrix & lhs, const Matrix & rhs );
    and you'd have a constructor:
    Code:
    Matrix(  const MatrixList & );
    The point is that it would then be quicker to add together multiple matrices thus:

    S = A + B + C + D;

    where they are all matrices, with much reduced allocations.

    Note: this approach can be templated and forms a template pattern.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured