How to Overload Operator==() for a Pointer to the Class

How to overload operator==() for a pointer to the class?

No, there is not.

To overload operator==, you must provide a user-defined type as one of the operands and a pointer (either AString* or const char*) does not qualify.

And when comparing two pointers, the compiler has a very adequate built-in operator==, so it will not consider converting one of the arguments to a class type.

C++ passing overloaded operator() of class as function pointer

Supposing that you have to use a function pointer, and that your functor has no state, you can use a lambda as glue:

void takesFunctionPointer(void (*)());

struct MyFunctor {
void operator()();
};

// ...

takesFunctionPointer([] { return MyFunctor{}(); });

overloading = operator to compare pointers

You almost always want to compare actual Point objects, not a pointer to a Point.

bool Point::operator>=(Point const & p) {
std::cout << "overloaded>=\n"; // should now print something
return x >= p.x && y >= p.y;
}

Then you'd invoke it like:

int main() {
Point p1{5, 5};
Point p2{4, 4};
std::cout << std::boolalpha << (p1>=p2) << '\n';
}

As a side note, if you support comparison in C++, it's (much) more common to overload operator< instead. By default standard algorithms will compare for less than, rather than greater than/equal to.

But, if you do decide to implement operator< for use with standard algorithms, you'll have to ensure that it carries out a "strict weak" comparison, which your current comparison is not (as it stands now, there are values of A and B (e.g., {4,5} and {5,4}) for with both A>=B and B>=A will return false, indicating that A is neither less than, nor equal to, nor greater than B. A comparison operator like that can (and often will) produce undefined behavior from things like sorting algorithms.

c++ Operator overloading in derivated class using pointers to base class

Your problem lies in the assumption that when VarInt inherits from base, then:

virtual base& operator = (const base& other) {};

and

virtual VarInt& operator = (const VarInt& other) {};

are the the exact same thing. Unfortunately, they are not. What you do here, is hiding the previous method. When you override a virtual function you need to specify it the exact same way it was firstly declared, that is:

virtual base& operator = (const base& other) {};

The returned base& can be bounded to VarInt object, so we are safe here. The provided argument, being const base& can also be referenced to a VarInt object. Now you have to think about what you actually want to achieve here. Assuming that from your base class multiple classes with different functionalities can inherit, what will happen if you, for example, try to add VarFloat to VarInt? What is the expected result? Compiler error? Throwing an exception?

If you really wish to enable operator = to work with all the permutations of your inherited classes, then using dynamic_cast<> and static_cast<> will do the trick, however - keep in mind that if you happen to need to use casts, you might wish to rethink your design. Their usage is not recommended.

For a working example with ...cast usage, see here:

class Base{
public:
// mandatory for destructing the objects via pointer of a base class
virtual ~Base() = default;

virtual Base& operator = (const Base& other) {};
};

class VarInt : public Base{
public:
virtual Base& operator = (const Base& other) override
{
const auto ptr = dynamic_cast<const VarInt*>(&other);
if(ptr != nullptr){
// do whatever you need to - the *other* object is of a type VarInt!
// use *ptr* to use *other* as VarInt
return *this;
} else {
// do whatever you need to, when *other* is NOT a VarInt!
// throw an exception? Add nothing?
}
}
};

Please keep in mind that this is not recommended for everyday usage - casts are a sign of a design flaw and should not be preferred

Pointer to a class with array indexing operator overload

With the following declaration:

basicVector *a = new basicVector(10);

You could dereference the pointer (preferred):

uint8_t n = (*a)[5];

Or call the operator using the operator syntax:

uint8_t n = a->operator[](5);


Related Topics



Leave a reply



Submit