Tempate Operator Question

Discussion in 'C++' started by sam, Jun 27, 2003.

  1. sam

    sam Guest

    If I pass class or struct to sum class, I have to overload "+" and <<
    operator for all types except arithmetic operator and STL. Is it correct? Is
    there any other way I can do this? Thank you very much.

    template<class C> class sum{
    private:
    C T, T1;
    public:
    sum(C,C);
    void display();
    };
    template<class C>
    sum<C>::sum(C a, C b){
    T=a;
    T1=b;
    }

    template<class C>
    void sum<C>::display(){
    C t2 = T+T1; //
    cout << t2 <<"\n";
    }

    typedef struct st{
    string Name;
    int age;
    } ST;

    int main(){
    typedef sum<int> SINT;
    sum<int> s1(100,100);
    ST st1,st2;
    st1.Name="Name1";
    st1.age =10;
    st2.Name="Name2"
    st2.age =20;
    sum<string > *s2 = new sum<string>("Hello","How are you?");
    sum<ST> stsum = new sum<ST>(st1,st2); // I am getting compiler error
    s1.display();
    s2->display();
    }

    Sam
     
    sam, Jun 27, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SGFyb2xk?=

    Object Based Datagrid Tempate column binding

    =?Utf-8?B?SGFyb2xk?=, Apr 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    442
    avnrao
    Apr 28, 2004
  2. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    611
    Rob Williscroft
    Mar 5, 2004
  3. John Smith
    Replies:
    2
    Views:
    440
    Ivan Vecerina
    Oct 6, 2004
  4. Alex Vinokur
    Replies:
    4
    Views:
    3,072
    Peter Koch Larsen
    Nov 26, 2004
  5. Alex Vinokur
    Replies:
    3
    Views:
    5,056
    Jeff Schwab
    Mar 20, 2005
Loading...

Share This Page