+ 2

Challenge question

Looking at this code https://code.sololearn.com/cZ6KwSDW32sz/?ref=app we would expect one of the both constructors to be called, but neither is called. What is going on?

2nd Jun 2018, 2:16 PM
Max
Max - avatar
7 Answers
+ 2
Max compiler see A a(); like a simple function declaration not like you think (constrution of an object a of type A) for this it dont give you an error... For rest your compiler cannot resolve ambiguity for a default constructed A object because of second constructor with default param
2nd Jun 2018, 9:25 PM
KrOW
KrOW - avatar
+ 1
KrOW thanks, that resolves all questions regarding this behavior. for anyone else every coming across this in the future: google "the most vexing parse"
2nd Jun 2018, 9:32 PM
Max
Max - avatar
+ 1
Max You are welcome 👍👍👍
2nd Jun 2018, 9:33 PM
KrOW
KrOW - avatar
0
There is no constructor called because the compiler cannot tell which one to call, both constructors could be possible. Since it doesn't know what to call, the compiler ends up calling none.
2nd Jun 2018, 3:26 PM
Shadow
Shadow - avatar
0
Naitomea how does this result in well defined behavior(what happens if i try to use the object)? and why does it even compile?and do you have sources for that? is this behavior in the standart?
2nd Jun 2018, 3:30 PM
Max
Max - avatar
0
I assume it compiles because you don't use the object. Usually, the compiler will complain when encountering ambigous calls, example: https://stackoverflow.com/questions/40194842/c-call-of-overloaded-constructor-is-ambiguous Once you try using the object, you will definitly get an error message: https://code.sololearn.com/cN9SSnXU50Zg/?ref=app Also I'm sorry, I don't know whether this is in the standard or not; I just encountered this problem once before.
2nd Jun 2018, 3:55 PM
Shadow
Shadow - avatar
0
Naitomea thanks, i think we did the same c++ challenge found some more sources: https://stackoverflow.com/questions/40194842/c-call-of-overloaded-constructor-is-ambiguous seems like most compilers would reject the code, even if you don‘t try to call a member function, sololearns olayground is just special
2nd Jun 2018, 3:56 PM
Max
Max - avatar