C++ Template problem adding two data types

后端 未结 9 1313
慢半拍i
慢半拍i 2021-02-13 02:51

I have a template class with an overloaded + operator. This is working fine when I am adding two ints or two doubles. How do I get it to add and int and a double and return th

9条回答
  •  孤城傲影
    2021-02-13 03:33

    You can add int and double values by using templates.In the function, specify 2 arguments and while passing values to the functions specify its types in angular brackets.

    example:

    //template
    template
    void add(T1 a, T2 b)
    {
        //for adding values
        cout<<"\n\nSum : "<(4,5.5454);
        add(4.7,5);
        add("hi","bye");
        return 0;
    }
    

提交回复
热议问题