Re: Why do single-argument constructors act as implicit type conversion functions by default
On 21 juin, 09:06, Andrew Tomazos <and...@tomazos.com> wrote:
class D : B {...}
void f(B*);
D* p = ...;
f(p); // and not f((B*)p);
...is okay, because the programmer reading the code should really know
what the base classes of the types he is looking at are
This is really not consistent.
It's ok when it is a native implicit conversion, but not when it is
emulated? Likewise, the programmer should really know what the
"logical base classes" (i.e. the types that implement similar
semantics as base classes) of the types he is looking at are.
and when f
is called no conversion is taking place, nothing magic is happening.
The pointer to D* is placed on the stack and f is called. All the B
methods just operate on the D instance like it was a B instance
That's not really how it works, no. Due to multiple and virtual
inheritance, upcasting is not necessarily a no-op. (and it's the same
in Java, of course, since it still has multiple inheritance, even if
it is limited to interfaces)
This is a distinct situation from calling silently implicitly calling
some constructor to some overloaded function, (even just a smart
pointer's constructor), and executing some code that could be from
anywhere.
No it isn't.
There should be no distinction between an actual pointer and a type
designed to act like a pointer.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]