+ 10

I need explanation why this code print output 1?

#include <iostream> struct A { int x; A(): x(0) {cout << "Constructor";) friend std::ostream& operator <<(std::ostream& stream, const A& obj) { return stream << obj.x; } }; int main() { A a(); cout << a << endl; }

14th Dec 2018, 1:42 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
10 Antworten
+ 12
Very interesting! "For some reason, using A a(); is different than using A a;" Well done Zeke! You have perfectly narrowed down the issue. Now, let's take a closer look... A a; VS. A a(); `A a;` tells us that you are instantiating a new object of type A. `A a();` tells us that you are prototyping a function called `a` that has no argument and return an object of type A. What happens if you run the above code? In GCC, you end up with a warning in regards to the `return address of a` like so warning: the address of 'A a()' will always evaluate as 'true' [-Waddress] In VS, you end up with a linker error (LNK 2019) like so LNK2019 unresolved external symbol "struct A __cdecl a(void)"
14th Dec 2018, 6:05 AM
Babak
Babak - avatar
+ 9
It's interesting that when you remove the parentheses from the instantiation of a, it will run as expected. For some reason, using A a(); is different than using A a; I know there's a rule about this somewhere in my head, but there are so many rules in so many languages that it starts to get all fuzzy. I think calling the constructor with no arguments using the parentheses is not good practice, probably for this reason. That's usually reserved for uses like: A b = new A(); Any other C++ people want a stab at this? https://code.sololearn.com/c1chZi2ruHm9 https://stackoverflow.com/questions/5116541/difference-between-creating-object-with-or-without/5116618
14th Dec 2018, 5:12 AM
Zeke Williams
Zeke Williams - avatar
+ 7
"But then why does it output 1?" Short answer: Because the address of a function is a non-zero value like 0xBAADF00D, and each non-zero value is considered `true` in terms of boolean.
14th Dec 2018, 7:00 PM
Babak
Babak - avatar
+ 7
Thanks for all your help C++ Soldier (Babak), Zeke Williams, Ketan Lalcheta, and the others.. Its solved my problem.
18th Dec 2018, 12:54 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 6
Zarthan Maybe C++ Soldier (Babak) can help you with that...
14th Dec 2018, 2:39 AM
Ikechukwu Okonkwo
Ikechukwu Okonkwo - avatar
+ 6
Ketan Lalcheta Yeah buddy! we usually get told to use language structures in the right place but we also know how flexible C++ is and many times it surprises us. It happens to the best of us! 8D
14th Dec 2018, 6:32 AM
Babak
Babak - avatar
+ 4
How I wish I know C++ language😢😢😢😢😢.. I'm so lost reading all these comments...
14th Dec 2018, 6:08 AM
Ikechukwu Okonkwo
Ikechukwu Okonkwo - avatar
+ 3
😎iYkE4LiFe😎 LOL! I promise you get to grip with all of it prety sooooon!
14th Dec 2018, 6:12 AM
Babak
Babak - avatar
+ 3
wow... Difference between A a(); and A a;... this is what I was actually looking for since so many days... C++ Soldier (Babak) , I tried to create class A as empty nd wrote A a(); in main function. this I did in Visual Studio 2012 and build successfully... A a() to be considered as function prototyping is another thing I could not get... in main function, we generally don't do function prototyping...
14th Dec 2018, 6:17 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
C++ Soldier (Babak) But then why does it output 1? Shouldn't it have thrown some garbage because of printout out an undefined function?
14th Dec 2018, 6:42 PM
Aditya
Aditya - avatar