I want to make a typedef struct called pos
(from position) that stores coordinates x and y. I am trying to overload some operators for this struct, but it does not compile.
typedef struct { int x; int y; inline pos operator=(pos a) { x=a.x; y=a.y; return a; } inline pos operator+(pos a) { return {a.x+x,a.y+y}; } inline bool operator==(pos a) { if (a.x==x && a.y== y) return true; else return false; } } pos;
I also wanted to know the difference between this:
inline bool operator==(pos a) { if(a.x==x && a.y== y) return true; else return false; }
And this:
bool operator==(pos a) const { if(a.x==x && a.y== y) return true; else return false; }
The breakdown of your declaration and its members is somewhat littered:
Remove the typedef
The typedef
is neither required, not desired for class/struct declarations in C++. Your members have no knowledge of the declaration of pos
as-written, which is core to your current compilation failure.
Change this:
typedef struct {....} pos;
To this:
struct pos { ... };
Remove extraneous inlines
You're both declaring and defining your member operators within the class definition itself. The inline
keyword is not needed so long as your implementations remain in their current location (the class definition)
Return references to *this
where appropriate
This is related to an abundance of copy-constructions within your implementation that should not be done without a strong reason for doing so. It is related to the expression ideology of the following:
a = b = c;
This assigns c
to b
, and the resulting value b
is then assigned to a
. This is not equivalent to the following code, contrary to what you may think:
a = c; b = c;
Therefore, your assignment operator should be implemented as such:
pos& operator =(const pos& a) { x = a.x; y = a.y; return *this; }
Even here, this is not needed. The default copy-assignment operator will do the above for you free of charge (and code! woot!)
Note: there are times where the above should be avoided in favor of the copy/swap idiom. Though not needed for this specific case, it may look like this:
pos& operator=(pos a) // by-value param invokes class copy-ctor { this->swap(a); return *this; }
Then a swap method is implemented:
void pos::swap(pos& obj) { // TODO: swap object guts with obj }
You do this to utilize the class copy-ctor to make a copy, then utilize exception-safe swapping to perform the exchange. The result is the incoming copy departs (and destroys) your object's old guts, while your object assumes ownership of there's. Read more the copy/swap idiom here, along with the pros and cons therein.
Pass objects by const reference when appropriate
All of your input parameters to all of your members are currently making copies of whatever is being passed at invoke. While it may be trivial for code like this, it can be very expensive for larger object types. An exampleis given here:
Change this:
bool operator==(pos a) const{ if(a.x==x && a.y== y)return true; else return false; }
To this: (also simplified)
bool operator==(const pos& a) const { return (x == a.x && y == a.y); }
No copies of anything are made, resulting in more efficient code.
Finally, in answering your question, what is the difference between a member function or operator declared as const
and one that is not?
A const
member declares that invoking that member will not modifying the underlying object (mutable declarations not withstanding). Only const
member functions can be invoked against const
objects, or const
references and pointers. For example, your operator +()
does not modify your local object and thus should be declared as const
. Your operator =()
clearly modifies the local object, and therefore the operator should not be const
.
Summary
struct pos { int x; int y; // default + parameterized constructor pos(int x=0, int y=0) : x(x), y(y) { } // assignment operator modifies object, therefore non-const pos& operator=(const pos& a) { x=a.x; y=a.y; return *this; } // addop. doesn't modify object. therefore const. pos operator+(const pos& a) const { return pos(a.x+x, a.y+y); } // equality comparison. doesn't modify object. therefore const. bool operator==(const pos& a) const { return (x == a.x && y == a.y); } };
EDIT OP wanted to see how an assignment operator chain works. The following demonstrates how this:
a = b = c;
Is equivalent to this:
b = c; a = b;
And that this does not always equate to this:
a = c; b = c;
Sample code:
#include <iostream> #include <string> using namespace std; struct obj { std::string name; int value; obj(const std::string& name, int value) : name(name), value(value) { } obj& operator =(const obj& o) { cout << name << " = " << o.name << endl; value = (o.value+1); // note: our value is one more than the rhs. return *this; } }; int main(int argc, char *argv[]) { obj a("a", 1), b("b", 2), c("c", 3); a = b = c; cout << "a.value = " << a.value << endl; cout << "b.value = " << b.value << endl; cout << "c.value = " << c.value << endl; a = c; b = c; cout << "a.value = " << a.value << endl; cout << "b.value = " << b.value << endl; cout << "c.value = " << c.value << endl; return 0; }
Output
b = c a = b a.value = 5 b.value = 4 c.value = 3 a = c b = c a.value = 4 b.value = 4 c.value = 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With