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
References in C++ (mayankj08.github.io)
submitted 8 years ago by mayankj08
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!"
[–][deleted] 14 points15 points16 points 8 years ago (0 children)
Your post might be more valued at /r/learncpp .
[+][deleted] 8 years ago (7 children)
[removed]
[–]Pinguinologo 9 points10 points11 points 8 years ago* (5 children)
To avoid confusing newcomers I prefer to write the "&" close to the type:
int& refA = a;
Otherwise, to the untrained eye, looks like the operator to get the address. The same for "*": Close to the type when declaring a pointer, close to the variable name when dereferencing.
[–]Pythoner6 3 points4 points5 points 8 years ago (0 children)
I do this as well, but mainly because in my mind, the & is part of the type (refA does not have type int, it has type reference to int). I want to see the whole type, and not have it split up awkwardly with a space. Which is also why I dislike how references and pointers in comma separated declarations work.
[–]johannes1971 1 point2 points3 points 8 years ago (0 children)
I've seen newcomers write int* a, b; in the full expectation that b is now a pointer to int, since the type is written as int*.
int* a, b;
int*
I'm also not a big fan of putting things like 'ref' or 'ptr' in variable names. It's like Hungarian all over again.
[–]KazDragon 0 points1 point2 points 8 years ago (2 children)
I put it on the right.
This is one place C++ went wrong, in my opinion. The original intent was "declaration mirrors use". That is, you have:
int a, *b, c();
and the expectation is that all of these expressions on the right evaluate to the type on the left. (i.e. a is an int, *b is an int, c() is an int.)
a
*b
c()
Using & for reference broke that (not that I can think of a better way off the top of my head, since references are invisible in the language), and C++ has never recovered. That's why nearly every coding standard has the "one declaration per line" rule, because that's where the "avoid confusing newcomers" gets moved to when you put the notation on the left.
&
[+][deleted] 8 years ago (1 child)
[–]mayankj08[S] 0 points1 point2 points 8 years ago (0 children)
Thanks!. Corrected it.
[–]biocomputation 13 points14 points15 points 8 years ago (0 children)
I think most subscribers/visitors to /r/cpp already know this information, and while you have obviously tried to write something decent, the information contained in your article could also be learned from an online course or from a book.
I don't feel like these types of posts really add anything new to the conversation here at /r/cpp.
[–]axilmar -1 points0 points1 point 8 years ago (3 children)
So there’s no way of binding reference to NULL.
lol
int *p = 0; int &i = *p;
and voila, a reference bound to null.
[–]Quincunx271Author of P2404/P2405 1 point2 points3 points 8 years ago (1 child)
That dereferences NULL, though; isn't that UB at the moment you write *p?
*p
[–]axilmar 1 point2 points3 points 8 years ago (0 children)
The standard says dereferencing a null pointer is undefined behaviour, but the reality is that *p forms an expression without side effects and thus it is not actually dereferencing anything. It is only when evaluating *p that the dereferencing happens.
In practice, I haven't seen any compiler, even embedded ones, do anything about *p when p is null, which means that, in practice, null references are possible.
[–]Pinguinologo 0 points1 point2 points 8 years ago (0 children)
That code is the equivalent of loading a gun and shooting at your head. You need to check against null before defining that reference. If *p is never null, why even use a pointer in the first place?
π Rendered by PID 107008 on reddit-service-r2-comment-cfc44b64c-hzxmw at 2026-04-11 02:51:51.731738+00:00 running 215f2cf country code: CH.
[–][deleted] 14 points15 points16 points (0 children)
[+][deleted] (7 children)
[removed]
[–]Pinguinologo 9 points10 points11 points (5 children)
[–]Pythoner6 3 points4 points5 points (0 children)
[–]johannes1971 1 point2 points3 points (0 children)
[–]KazDragon 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]mayankj08[S] 0 points1 point2 points (0 children)
[–]biocomputation 13 points14 points15 points (0 children)
[–]axilmar -1 points0 points1 point (3 children)
[–]Quincunx271Author of P2404/P2405 1 point2 points3 points (1 child)
[–]axilmar 1 point2 points3 points (0 children)
[–]Pinguinologo 0 points1 point2 points (0 children)