+ 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?
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
+ 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"
+ 1
Max You are welcome đđđ
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.
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?
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.
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