Is here a difference between Box operator + (const Box& box2){"overload code"}
and Box operator +(Box const& box2){"overload code"}
if you have a custom class called Box? And if so, what is the difference?
Asked
Active
Viewed 92 times
1

1201ProgramAlarm
- 32,384
- 7
- 42
- 56

edo101
- 629
- 6
- 17
-
3No they're exactly the same. – john Apr 26 '20 at 21:26
-
No, there's no difference. – Igor Tandetnik Apr 26 '20 at 21:26
-
`const Box&` could be read as "a reference to a `Box` that is constant", and `Box const&` could be read as "reference to a constant `Box`. Semantically exactly the same. – Some programmer dude Apr 26 '20 at 21:28
-
What if it was (Box const &box2) or (const Box &box2), is there any difference? between it and the first two? @john – edo101 Apr 26 '20 at 21:32
-
Spaces are irrelevant here. You can have `const Box& box1`, `const Box &box1`, `const Box & box1`, or even add newlines or tabs instead of spaces. Any decent book, tutorial or class should have taught you that. – Some programmer dude Apr 26 '20 at 21:35
-
@Someprogrammerdude unfortunately my begineers class professor wasn't the type to explain basics like that without someone asking a question. – edo101 Apr 26 '20 at 21:43
-
1@edo101 No worries. Bad teachers can't stop our thirst for knowledge. Go ahead and see [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/10147399). Pick up something for beginners and you'll be golden. I recommend [C++ Primer](https://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113), it's really well written. – Aykhan Hagverdili Apr 26 '20 at 21:49
-
@Ayxan thanks for the suggestion. Yeah for me I am learning way more outside of class as I youtube video tutorials and try to follow – edo101 Apr 26 '20 at 22:53
1 Answers
0
There is no difference. Those two are identical. int const&i
, int const& i
, int const &i
, int const & i
, const int&i
, const int &i
, const int& i
, const int & i
all have the exact same type: reference to a constant int
.

Aykhan Hagverdili
- 28,141
- 6
- 41
- 93
-
What if it was (Box const &box2) or (const Box &box2), is there any difference? between it and the first two @Ayxan – edo101 Apr 26 '20 at 21:32
-
-
so (Box const &box2) and (Box const& box2) are the same thing which are the same as (const Box& box2) and (const Box &box2)? @Ayxan – edo101 Apr 26 '20 at 21:36
-
Yes, exactly. The placement of `const` before or after the type, or the existence of white space before or after `&` does not change anything whatsoever. – Aykhan Hagverdili Apr 26 '20 at 21:37
-
thank you @Ayxan. How would you read it out loud? "referene to a constant box? " – edo101 Apr 26 '20 at 21:42
-
Yes, exactly. You could also say "reference to a const box" or a "const box reference" and everyone will still understand. – Aykhan Hagverdili Apr 26 '20 at 21:43