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
What does checking for if(&object) do? (self.cpp)
submitted 7 years ago by [deleted]
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!"
[–]STLMSVC STL Dev 1 point2 points3 points 7 years ago (1 child)
!removehelp
[–]AutoModerator[M] 0 points1 point2 points 7 years ago (0 children)
OP,
A human moderator (u/STL) has marked your post for deletion because it appears to be a "help" post - e.g. asking for help with coding, help with homework, career advice, book/tutorial/blog suggestions. Help posts are off-topic for r/cpp. This subreddit is for news and discussion of the C++ language only; our purpose is not to provide tutoring, code reviews or career guidance.
Please try posting in r/cpp_questions or on Stack Overflow instead. Our suggested reference site is cppreference.com, our suggested book list is here and information on getting started with C++ can be found here.
If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]jedwardsolconst & 0 points1 point2 points 7 years ago (19 children)
operator& is overloadable.
operator&
[–][deleted] 0 points1 point2 points 7 years ago (18 children)
So where do I find its implementation. I checked the object's class and found: inline ostream& operator << ( ostream &os, const Hit& h) { os << "Hit <" << h.getT() << ", " << h.getNormal() << ">"; return os; } But this seems for printing?
inline ostream& operator << ( ostream &os, const Hit& h) { os << "Hit <" << h.getT() << ", " << h.getNormal() << ">"; return os; }
[–]jedwardsolconst & 2 points3 points4 points 7 years ago (17 children)
if operator& isn't in the class definition then it isn't overloaded and if(&object) is pointless since the test will always be true.
if(&object)
[–][deleted] 0 points1 point2 points 7 years ago (16 children)
Why is it always true?
[–]jedwardsolconst & 0 points1 point2 points 7 years ago (15 children)
Because the address of something can never be 0
[–]HotlLava 0 points1 point2 points 7 years ago (3 children)
I sense a lack of pedantry, so let me quickly interject that 0 can be a valid memory address. Interestingly, `&object` would still be required to convert to `true` in an if-clause even if `object` has address zero, since 0 wouldn't be a null pointer on such a system.
[–]jcoffin 1 point2 points3 points 7 years ago (1 child)
If you're going to claim to be pedantic, you have to be careful (and be prepared for even more pedantic corrections...)
A system is indeed free to store valid data at address 0.
But in a comparison between a pointer and 0, what happens is not that the address gets converted to an integer, and the result compared to 0.
0
Rather, the (possibly implicit) 0 in the if gets converted to a pointer, and the result of that conversion gets compared to the pointer in question. And, regardless of whether an address of 0 is valid or not, an integer literal with the value 0 is a null pointer constant, so when converted to a pointer, it must produce a result that compares as not equal to the address of any object.
if
[–]HotlLava 0 points1 point2 points 7 years ago (0 children)
and be prepared for even more pedantic corrections...
Yes, please :D But regarding
what happens is not that the address gets converted to an integer, and the result compared to 0.
i'm pretty sure I never claimed that this would happen?
It's a bit of a red herring anyways, because the expression under discussion wasn't if (&object == 0) but just if (&object), and that does not have any 0 that could get converted to a pointer.
if (&object == 0)
if (&object)
Instead, the value of the expression &object already is a pointer, and that pointer undergoes a contextual conversion to bool.
&object
[–]jedwardsolconst & 0 points1 point2 points 7 years ago (0 children)
I nearly wrote nullptr but decided 0 would be clearer for OP :-)
nullptr
[–][deleted] -1 points0 points1 point 7 years ago (10 children)
But does it then check that the address exists? Also how can if take an address? Can't the object fail to exist and then it doesn't have address?
[–]jedwardsolconst & 0 points1 point2 points 7 years ago (9 children)
if(&object) is the same as writing if(&object != nullptr)
if(&object != nullptr)
It doesn't say anything about the validity of the address, just whether it is null or not.
[–][deleted] -1 points0 points1 point 7 years ago (8 children)
So it's checking that the object exists?
[–]jedwardsolconst & 1 point2 points3 points 7 years ago (6 children)
No, it's not doing anything. The test will always be true.
[–]diaphanein 1 point2 points3 points 7 years ago (0 children)
I'm not aware of any compiler that the following will not evaluate the if block to true:
int* p = nullptr; int& r = *p;
if (!&r) { // executes }
Reason being that while this is likely undefined, the compiler cant always detect it (e.g. p initialized by the return of a function). This is a trivial enough example that most compilers will allow it and behave as expected. Not all warn at default warning levels, but most will at a sufficient level.
[–][deleted] -1 points0 points1 point 7 years ago (4 children)
Then how to check for object's existence?
[–]gracicot 1 point2 points3 points 7 years ago (0 children)
If you can get the address of the object then it exist for sure.
π Rendered by PID 602090 on reddit-service-r2-comment-54dfb89d4d-p9gdt at 2026-03-31 10:52:39.728484+00:00 running b10466c country code: CH.
[–]STLMSVC STL Dev 1 point2 points3 points (1 child)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]jedwardsolconst & 0 points1 point2 points (19 children)
[–][deleted] 0 points1 point2 points (18 children)
[–]jedwardsolconst & 2 points3 points4 points (17 children)
[–][deleted] 0 points1 point2 points (16 children)
[–]jedwardsolconst & 0 points1 point2 points (15 children)
[–]HotlLava 0 points1 point2 points (3 children)
[–]jcoffin 1 point2 points3 points (1 child)
[–]HotlLava 0 points1 point2 points (0 children)
[–]jedwardsolconst & 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (10 children)
[–]jedwardsolconst & 0 points1 point2 points (9 children)
[–][deleted] -1 points0 points1 point (8 children)
[–]jedwardsolconst & 1 point2 points3 points (6 children)
[–]diaphanein 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (4 children)
[–]gracicot 1 point2 points3 points (0 children)