incomplete / useless method?

Pages: 12
Hey guys,

can you make any sense of the following... method?

1
2
3
4
Obstacle & Obstacle::operator= (const Obstacle & other){
  segs=other.segs;
  return *this;
}


Obstacle is a class with the following header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "segment.h"
#include <vector>
#include <iostream>
class Obstacle {
 public:
  std::vector<segment> segs;
  Obstacle();
  Obstacle(double w, double l, double centerx, double centery);
  void manipulate(double dx, double dy, double dtheta);//this is something
  void print();
  void print(std::string iname);  
  Obstacle & operator= (const Obstacle & other);
 /* friend: */
 /*  std::ostream& operator<<(std::ostream& os, const Obstacle& obst); */

Segment is another class.

What does the method do? Is it even legit C++?
Last edited on
its an assignment operator.
so you can say
Obstacle x, y;
... stuff
x = y;


c++ allows you to overload the operators. The inputs they take are rigid (you can't make operator = (int x, double d, aclass c) for example. But you can make it do just about anything you want as long as the format for the parameters is correct, it does not have to do assignment (yours does). I have a ! logical not operator that stands for transpose for my matrix class, for an example of making it do something else.

the compiler can sometimes make the assignment operator for you, so it is often redundant. I am not sure if you really, really NEED it here or not. Google the rule of big 3 as well.
Last edited on
It's an overloaded assignment operator which sets the object to the value of the other object (by copying the vector that represents its state).

Why do you think there's a problem with that?
the compiler can sometimes make the assignment operator for you, so it is often redundant. I am not sure if you really, really NEED it here or not.

It looks like it's doing the exact same thing as the compiler generated copy assignment operator would have done, except that it prevents the move constructor and move assignment operator from being automatically generated.
Last edited on
In other words, one could achieve the same with:
1
2
3
4
5
class Obstacle {
 public:
  Obstacle & operator= (const Obstacle &) = default;
  // other members
};
Hey guys,

thanks for the responses.
So I am overloading the "=" operator for the class obstacle.
What does the syntax mean? Obstacle & Obstacle::operator= (const Obstacle & other)
The "&" is the address operator right? So the operator is not defined for the actual obstacle but for obstacle pointers?

"const Obstacle &other" is a pointer to a constant obstacle? What if my obstacle is not constant?
Read about references.
The "&" is the address operator right?

When used as a unary operator, then, yes, it's the address operator.

However, it couldn't possibly make sense for it to be an operator here. It's being used as part of the signature of a method definition, specificly as part of declaring a type. When used in a declaration, it indicates that the type of the thing being declared is a reference:

https://www.tutorialspoint.com/cplusplus/cpp_references.htm


The "&" is the address operator right?
No, in this context it's the reference operator.

What if my obstacle is not constant?
When you pass an object as a const reference it just means that you promise that you don't change it in the function. It does not need to be constant anywhere else.
And in this particular case it means also that you cannot return it.
Ah so it was "passing by reference". I almost forgot about that, because the code I am working with now almost always passed pointers to an object...

So passing "const Obastacle& other" means I am working with the actual object (unlike a copy as in passing by value), but due to the "const", I promise that the object is not changed.

However, I never encountered the reference in the function name.
Obstacle & Obstacle::operator=
"Obstacle::operator=" is the technical name of the function?
And "Obstacle&" means the function returns an obstacle reference?
So passing "const Obastacle& other" means I am working with the actual object (unlike a copy as in passing by value), but due to the "const", I promise that the object is not changed.

Correct. It's largely to improve performance, as you don't need to copy objects. It also avoids any side-effects of invoking the copy constructor, if there are any.

And "Obstacle&" means the function returns an obstacle reference?

Correct. It's necessary, to allow you to chain assignments, e.g.

 
a = b = c;  // Where a, b and c are all of type Obstacle 


See https://stackoverflow.com/questions/3105798/why-must-the-copy-assignment-operator-return-a-reference-const-reference for more info.
Last edited on
Ok, if I have:

1
2
3
4
//assume we have defined obstacle a...

obstacle b;
obstacle b=a


The compiler calls "=" as a function of obstacle "b". "a" is the argument of that function and is passed by constant reference, meaning "a" will not be changed (as in usual assignment).
The function copies the properties of "a" to "b" and then returns a pointer to "b".

So "b" is a pointer to "b"? Ok, I don't get it :D
Why am I even returning a pointer, if the function signature expects just a reference?
and then returns a pointer to "b".

No. As we've already discussed, it returns a reference to b.

As for the "why", I've already covered that.
Last edited on
Note that there is a difference between
1
2
obstacle a;
obstacle b = a; // copy constructor 
and
1
2
3
obstacle a;
obstacle b;
b = a; // copy assignment operator 
.
Oops, good spot, Peter87! Yes - the line obstacle b=a; does not invoke the assignment operator!

obstacle b=a;

does the same thing as:

obstacle b(a);
Last edited on
Why am I even returning a pointer,

Where do you return a pointer?

In this?
1
2
  return *this;
}

Why do you think that to return a pointer?
@PhysicsIsFun

Just a minor point. I prefer to put & and * next to the type, like this:
 
Obstacle& operator= (const Obstacle& other);


https://isocpp.org/wiki/faq/style-and-techniques

isocppsuperfaq wrote:
A “typical C++ programmer” writes int* p; and explains it as “p is a pointer to an int:” emphasizing type. Indeed the type of p is int*. A C++ mindset prefers that emphasis and sees it as important for using the more advanced parts of C++ well.


This is a personal preference thing, but isocpp does give a reason for doing it that way.
TheIdeasMan wrote:
Just a minor point. I prefer to put & and * next to the type


Yeah, that's what I do, too. It makes the meaning clearer, to me.

However, you need to remember that the * is not actually part of the type; if you forget that, it can lead to some unexpected behaviour:

1
2
3
4
    int i = 1, j = 2;
    int* iPtr, jPtr; // What type is jPtr?
    iPtr = &i;
    jPtr = &j;  // What is the result of this? 

@MikeyBoy

Yes, they had that example on isocpp superfaq which I linked, the answer being to declare 1 variable per LOC.

Regards :+)
Oops... busted for not reading the link :)
Pages: 12