Questions about overloading operators

My main question is about when you must use "operator" and when you must use "&operator"

for example... if I have to add an issue to std::ofstream I should use a thing like that

1
2
3
4
5
6
7
8
9
class A {
 ...
 friend std::ofstream &operator<< (std::ofstream &, A &);
};

std::ofstream &operator<< (std::ofstream &out, A & a) {
  ....
  return out;
}


But why, in this case it is used "&operator"?

In other cases I see examples of using simply "operator" like

1
2
3
4
5
6
7
8
9
10
11
class A {
 ...
 public:
 A operator=(A);
}

A A:operator= (A a) {
  A tmp;
  ...
  return tmp;
}


in this example also the usage of parameter differs if compared with 1st example.
In 1st example I see that reference was used also for parameters (std::ofstream &out, A & a)... in the second example, instead, reference was NOT used at all, including parameters (A a).

Why?

It is possible to have a detailed explaination and suggestions when you have to use references (with operator itself and with parameters) and when not? So when to use "operator" and when "&operator" ; when to use #operator# (param normal) and when to use #operator# (param &reference)? [#operator# stands for operator# / &operator# where the final # stands for operator overloaded (ex. operator+)]

Sorry for bad english. Hoping that my question is understandable.
Regarding:
std::ofstream &operator<< (std::ofstream &, A &);

1. operator returns a reference to a stream because you don't want to return a copy, but the same one passed in so you can chain calls like:
std::cout << "hello" << ' ' << "world" << std::endl;

2. The stream operator shouldn't talk about ofstream, but the base class ostream so it goes to any ostream class incluing ofstreams.

3. The class to be streamed isn't meant to be modified, so it should be passed by const reference:
std::ostream &operator<<(std::ostream &, const A &);

4. A matrix + operation results in a new distinct matrix, so it will return a new value.
matrix operator+(const matrix &a, const matrix &b);
Last edited on
Thank you very much :)
1st of all for your corrections (even if I used ofstream only for example). They are very usefull to clarify better the usage of operators overloading

In second place thank for your explainations... if I understood well you use "&operator" instead of "operator" all times you require that the object returned is the same one passed and not a copy of the object. Did I understand Right?
Correct.
I always thought that way of putting the whitespace was confusing.

this makes so much more sense:

 
std::ofstream& operator << (std::ofstream &, A &);


The & is really with the ofstream, which indicates that you're returning a reference to an ofstream.

That's how I always thought of it anyway. & is part of the type, so it make sense to keep it with the type.

*shrug*

but that's just a personal preference.
My bad. It's an old habit from my C days. If you have multiple declarations on a line it seems sensible.
char *p, *q;

But I never make multiple declarations on the same time, so I don't know why it became a rule. Anyway, Alexandrescu gives a few good reasons it should the the way you've used. And I agree with him, but I still sometimes resort to the old ways.
Topic archived. No new replies allowed.