Skip to main content
GameDev.net gamedev.net
🔒 Locked

Constructors called automatically?

Started by snooty Sep 16, 2006 at 9:31 AM 2 replies 650+ views
Original Post
snooty
snooty
Hi. I ran into an interesting situation today: class A {...}; class B { B(const A &a) {...} //constructor void Foo(const B &b) {...} void FooFoo(const A &a) { Foo(a); } //!!!interesting!!! }; The above compiles fine with Visual Studio 2005. Does it mean the compiler is smart enough to invoke the constructor of B automatically in FooFoo? Or did the compiler overlook the error and I'll die at run-time?
Zao
Zao
Single argument constructors may participate in implicit type conversion, which might surprise some.
If you want to protect against something like this happening, declare the offending constructor explicit.
To make it is hell. To fail is divine.
incin
incin
Yes, the compiler is smart enough.
Use 'explicit' if you don't want that happening.
snooty
snooty
Thank you for the answers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.