Omkar
Omkar

Reputation: 831

Function template with operator overloading

I am learning templates and came across function templates.I have developed the following code out of my creativity or just curiosity.

#include<iostream>
using namespace std;
template <typename type>
type Max(type a, type b)
{
    return a > b ? a : b;
}
class Foo
{
public:
    string s1;
    /*
    Foo& operator>(Foo &obj)
    {
        if (this->s1.length() > obj.s1.length() ? true : false)
            return *this;
        return obj;
    }*/
};
int main()
{
    cout << Max(2, 3) << endl;
    cout << Max(2.0, 3.0) << endl;
    Foo a, b;
    a.s1 = "AB";
    b.s1 = "ABC";
    //cout<<Max(a, b).s1;
}

My idea is to pass Foo objects a and b using the Max function template and overload the '>' operator and print the object's string with greater length. Am i on the right path?Or should it be related to class templates?

Upvotes: 0

Views: 105

Answers (4)

Vincent_Bryan
Vincent_Bryan

Reputation: 178

#include <iostream>

using namespace std;

template <typename type>
type Max(type a, type b)
{
    return a > b ? a : b;
}

class Foo {
public:
    string s1; 
    bool operator > (Foo &obj) {
        return this->s1.length() > obj.s1.length();         
    }
};

int main()
{
    cout << Max(2, 3) << endl;
    cout << Max(2.0, 3.0) << endl;
    Foo a, b;
    a.s1 = "AB";
    b.s1 = "ABC";
    cout << Max(a, b).s1;
}

This maybe what you want: output results

Upvotes: 0

John Burger
John Burger

Reputation: 3672

The answer to your question is "Yes, you're on the right track."

The problem with generic functions is using them efficiently. Your Max() function works, but it copies Objects all over the place.

template <typename type>
const type &Max(const type &a, const type &b)
{
    return a > b ? a : b;
}

This solves the Object copying problem - but now it's less efficient for things like ints.

Upvotes: 2

songyuanyao
songyuanyao

Reputation: 172894

Let operator> return bool, otherwise it can't work with return a > b ? a : b;.

bool operator>(const Foo &obj)
{
    return this->s1.length() > obj.s1.length();
}

BTW1: if (this->s1.length() > obj.s1.length() ? true : false) is redundant.
BTW2: It's better to make the parameter type to const since obj won't and needn't be modified.

Upvotes: 2

Sam Varshavchik
Sam Varshavchik

Reputation: 118300

You're on the right path, except that the > operator should take a const reference parameter, itself be a const class method, and return a bool:

bool operator>(const Foo &obj) const;

After all, what do you expect the result of > to be, with anything else, for example:

double a, b;

// ...

auto whatisthis= a > b;

Do you expect whatisthis to be a double? Of course not, it will be a bool. The result of any comparison operator, not only > should be a bool. It doesn't really have to be, you can overload the > operator and have it return anything; but that means that you couldn't use the way you expect to use it.

Upvotes: 2

Related Questions