Download

==Operator overloading not working

I have been Googling for a few hours now and came up with this so far but its still not working. It may be something to do with the pointers as I am still learning C++.


//Photo.h
public:
    FORCEINLINE bool operator==(UPhoto &Other);


//Photo.cpp
bool UPhoto::operator==(UPhoto & Other)
{
    UE_LOG(LogTemp, Warning, TEXT("OPERATOR OVERLOADED!"));     // I never see this!!
    return ((PhotoGrade == Other.PhotoGrade) && (PhotoSubject == Other.PhotoSubject));
}


//MyGameState.cpp    
    //create temp array with removed duplicates
    TArray<UPhoto*> PhotosToAdd; //To add to master collection later
    for(UPhoto* p : RunPhotos)
    {
        if (!PhotosToAdd.Contains(p)) //should be using overloaded == but always returns false
        {
            PhotosToAdd.Add(p);
        }
    }

Any help appreciated.

  • Stef

Do “FORCEINLINE” operator overloading in the body of your class in .H file;
Never in CPP file.

Instead of



FORCEINLINE bool operator==(UPhoto &Other);


Just do it all right there instead:



FORCEINLINE bool operator == (const UPhoto &Other)
{
    //......
}


Thanks for the reply Bruno but this is exactly what I had in the first place… the separating out was a last ditch effort before posting here. I have put it back to the way you mention above and it is still not working. At least I am not getting the UE_LOG readout at all.


public:
    FORCEINLINE bool operator == (const UPhoto &Other)
    {
        UE_LOG(LogTemp, Warning, TEXT("OPERATOR OVERLOADED!"));
        return ((PhotoGrade == Other.PhotoGrade) && (PhotoSubject == Other.PhotoSubject));
    }

I have even tried directly testing (p == p) which quite rightly returns true but it does not use my overloading. Am I missing something else fundamental required for operator overloading?

-Stef

You’re working with an array of pointers, which means it will check if the pointer is contained in the array.

Thanks Zeblote, I am still working on that but I am currently more concerned that its not even trying to use my Overloaded operator. I am aware of some logical issues with this.
: )

[edit] unless you mean that is the reason for it not using my == operator?

If you’re using == with pointers, it will compare the pointers, not the objects pointed to

Sure, but it should still run my overloaded == to find out if the pointers are the same surely?

Ok, it looks like you nailed it Zeblote. By dereferencing I am now getting my overloaded operator called. Thanks! : )

-Stef

So I am still stuck on this, I can’t get the Contains() to use my == operator because its an array of pointers. Even if I use dereferencing it does not work. I have tried writing my own method for comparing Arrays but keep getting choked by not being able to resize an array inside a for loop.

I have written and rewritten various methods but all either don’t work because I’m using pointers or I go round in circles until I end up trying to resize an array inside a for loop again. So frustrating… what is the work around to this?

-Stef

You can use ContainsByPredicate and dereference it inside the lambda. I believe the syntax would be like this:



TArray<UBlah*> Things;
UBlah* ThingToCompare;

if(Things.ContainsByPredicate([ThingToCompare](const UBlah* Thing){ return (*ThingToCompare) == (*Thing); }))
{
    ...
}


That’s cool, I did wonder what that predicate stuff in the documentation was all about.

OK, I have got a test case working…



    while (PopMeArray.Num() > 0)
    {
        Popped = PopMeArray.Pop();
        if (!ReturnArray.ContainsByPredicate([Popped](const UMyObject* Ptr) { return (*Popped) == (*Ptr); }))
        {
            ReturnArray.Add(Popped);
        }
    }


now to try and get it working in my project proper!

Thanks again. :slight_smile:

-Stef