Thread: using templates to overload operators

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267

    using templates to overload operators

    Is it even possible? I have a c++ class that has quite a few overload operators with various parameters. example:
    Code:
    void operator+(int x);
    void operator+(long x);
    void operator+(unsigned int x);
    void operator+(unsigned long x);
    ...
    Is there a way to do the same thing with one template? If yes, could you post an example?

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I don't think overloading operators works on plain old data. If you wanted to convert plain old data to your class, then it would work.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I suppose you could do something like this:
    Code:
    class Foo
    {
    public:
      Foo():data(0){}
      Foo(int val):data(val){}
      
      template <class T>
      Foo operator+(T rhs) { return Foo(data+rhs); }
    
      void Print() { std::cout<<data<<std::endl;}
    private:
      int data;
    };
    
    
    
    int main()
    {
      Foo bar(6);
      (bar+5).Print();
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, since a cast is going to take place, anyway:

    Code:
    template <class Number>
    struct foo
    {
    	/*
    		constructors, destructor, etc...
    	*/
    
    	template <class Other>
    	foo<Number> &
    	operator += (const Other & rhs)
    	{
    		value += (Number)rhs;
    		return *this;
    	}
    	
    	template <class Other>
    	foo<Number>
    	operator + (const Other & rhs)
    	{
    		return foo<Number>(*this) += rhs;
    	}	
    	
    	Number value;
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overload Operators
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 11:13 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. How do I Overload these operators in this program?
    By advocation in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2005, 11:29 AM
  4. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  5. Replies: 5
    Last Post: 11-24-2002, 11:05 PM