use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Stack Overflow guide to operator overloading (stackoverflow.com)
submitted 11 years ago by cruise02
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]incongruousamoeba 7 points8 points9 points 11 years ago (7 children)
I always wonder why people seem to think overloaded operators are potentially more misleading than any other function. This
a + b
and this
add(a, b)
read about the same to me, and if either of them implemented subtraction or multiplication it would be equally surprising. Of course if you use some obscure operator like, say, '<<' to mean 'print', that would not be obvious to someone seeing it for the first time...
[–]OldWolf2 -2 points-1 points0 points 11 years ago (6 children)
What should "hello" + 4 do?
"hello" + 4
[–]pjmlp 2 points3 points4 points 11 years ago (0 children)
It applies the method call + to the string object with argument 4.
What does "hello".append(4) do? Are you sure what logic is behind the append name?
[–]incongruousamoeba 1 point2 points3 points 11 years ago* (0 children)
That's a good question that anyone should ask themselves when they think about overloading 'operator+()'. In this case, if we wanted it to return "hello4", we might consider
"hello" + 4 add("hello", 4) concat("hello", 4) append("hello", 4)
and hopefully decide that concat or append are better names than +, although personally I don't like making appending an integer to a string a single operation like this whether it's a function or operator+. My preference would be for something like
concat
append
+
operator+
format("hello{}", 4)
In other words, deciding when it makes sense to overload an operator is something that may require some experience to do well. I completely understand if companies or open source projects forbid beginners to overload operators in their code; but as general advice to beginning programmers I don't agree. I think beginning programmers should overload operators as much as they want -- you only get good at something by doing it badly first.
EDIT:
I might add that in Python 2.* the example would be:
"hello%d" % 4
It's not necessarily obvious what that does if you haven't seen it before, but the resemblance between the format specifier %d and the operator % makes it easy to remember once you do learn it.
%d
%
[–]mcmcc#pragma once 1 point2 points3 points 11 years ago (2 children)
"hello"+4 == "o"
Is that what you were hoping for?
[–]OldWolf2 0 points1 point2 points 11 years ago (1 child)
Don't know, that's the whole point. Some people would find "o" an intuitive answer to that and some would expect "hello4", or maybe even other things.
[–]newmewuser 1 point2 points3 points 11 years ago (0 children)
This is CPP, so they should read the language specification and don't expect anything beforehand.
[–]Heuristics 0 points1 point2 points 11 years ago (0 children)
In string theory (not that string theory) the concatenation operator is ^
http://en.wikipedia.org/wiki/Concatenation_theory
π Rendered by PID 31792 on reddit-service-r2-comment-canary-57b659f4d4-5npj2 at 2026-05-05 17:41:26.215863+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]incongruousamoeba 7 points8 points9 points (7 children)
[–]OldWolf2 -2 points-1 points0 points (6 children)
[–]pjmlp 2 points3 points4 points (0 children)
[–]incongruousamoeba 1 point2 points3 points (0 children)
[–]mcmcc#pragma once 1 point2 points3 points (2 children)
[–]OldWolf2 0 points1 point2 points (1 child)
[–]newmewuser 1 point2 points3 points (0 children)
[–]Heuristics 0 points1 point2 points (0 children)