Evan Nudd
Evan Nudd

Reputation: 216

Operator Overloading for struct pointers

I'm trying to compare two dynamically allocated struct pointers using operator overloading. It is to my understanding that operator overloads are supposed to be using pass by reference, so does that mean overloading the == operator to accomplish this particular goal isn't possible?

The goal is to print out Success without editing the code in main()

#include <iostream>


struct VAR
{
    int x;

VAR()
{
    x = 0;
}

VAR(int val)
{
    x = val;
}

bool operator==(VAR &lhs)
{
    std::cout << "operator called\n";
    return (x == lhs.x);
}

bool operator==(VAR *lhs)
{
    std::cout << "ptr operator called\n";
    return (x == lhs->x);
}
};

int main()
{
    VAR *v1 = new VAR(5);
    VAR *v2 = new VAR(5);

    if (v1 == v2)
        std::cout << "Success\n";
    else
        std::cout << "Fail\n";

    delete v1;
    delete v2;

    v1 = nullptr;
    v2 = nullptr;

    system("pause");
    return 0;
}

Upvotes: 0

Views: 1360

Answers (5)

BlackDwarf
BlackDwarf

Reputation: 2070

To complete the other answers, here is the part of the C++ standard that explicitly prohibits operator overloading with two pointers (emphasis mine):

An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators. The meaning of the operators = , (unary) & , and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator functions that implement these operators. Operator functions are inherited in the same manner as other base class functions.

[over.oper]/6

That explains why you can't use an overloaded operator== without changing the code in your main function.

Upvotes: 2

Christian Hackl
Christian Hackl

Reputation: 27548

Your == compares two pointers. Pointers are primitive types, regardless of what they point to, and you cannot overload operators when all operands are of primitive types.

In other words: Your overloaded operators work if the left operand is a VAR and not a VAR*.

Of course, your code does not make a convincing point for using pointers or dynamic memory allocation anyway. Just remove all of that:

int main()
{
    VAR v1(5);
    VAR v2(5);

    if (v1 == v2)
        std::cout << "Success\n";
    else
        std::cout << "Fail\n";
}

This way, you also don't need the second operator anymore. For the sake of completeness, you could invoke it like this:

int main()
{
    VAR v1(5);
    VAR v2(5);

    if (v1 == &v2)
        std::cout << "Success\n";
    else
        std::cout << "Fail\n";
}

Upvotes: 1

Benny Abramovici
Benny Abramovici

Reputation: 582

#include <iostream>


struct VAR
{
    int x;

VAR()
{
    x = 0;
}

VAR(int val)
{
    x = val;
}

bool operator==(VAR &lhs)
{
    std::cout << "operator called\n";
    return (x == lhs.x);
}

bool operator==(VAR *lhs)
{
    std::cout << "ptr operator called\n";
    return (x == lhs->x);
}
};

int main()
{
    VAR *v1 = new VAR(5);
    VAR *v2 = new VAR(5);

   if (v1->operator==(v2))
       std::cout << "Success\n";
    else
        std::cout << "Fail\n";

    delete v1;
    delete v2;

    v1 = nullptr;
    v2 = nullptr;


    return 0;
}

Upvotes: -1

Some programmer dude
Some programmer dude

Reputation: 409482

Consider the following code, almost the same as yours but without pointers:

VAR v1(5);
VAR v2(5);

if (v1 == v2) { ... }

That condition is actually handled by the compiler as

if (v1.operator==(v2)) { ... }

When you turn v1 into a pointer, the compiler doesn't automatically dereference the pointer, i.e. it doesn't to

v1->operator==(v2)

The simple solution is to dereference the pointers yourself:

*v1 == *v2

Upvotes: 2

EylM
EylM

Reputation: 6113

The common implementation of == operator would be

bool operator==(const VAR & lhs)

Even if you allocate a point to VAR object, you can "call" the == operator using * operator.

VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);

if (*v1 == *v2)
    std::cout << "Success\n";
else
    std::cout << "Fail\n";

Upvotes: 1

Related Questions