X
xllx.relient.xllx
This is a true or false question; if false, please explain why it is
so:
In main(...), the call to Handler with an argument will first call the
default ctor of the class which returns a temporary object and, then it
will call the copy ctor to copy the temp object to the object in the
Handler definition argument. So all in all, it calls two ctors, the
default and the copy ctor, respectively. Now, if this is true, can
someone explain why the default ctor only gets called in my compiler -
even though I've set optimizations to "Disabled (/Od)" in my VSC++ 2005
IDE? Could it be that this optimization is not an option to turn off or
on and that's why only the default ctor is called?
class Foo {
public:
Foo(const Foo& string) {
std::cout<<"copy-ctor"<<std::endl;
}
Foo() {
std::cout<<"def-ctor"<<std::endl;
}
};
void Handler(Foo string) {
}
int main(int argc, char** argv)
{
Handler(Foo());
return 0;
}
[p.s, I know the standard allows copy-ctors to be bypassed on unnamed
temp objects, which would explain my result but I've set optimizations
under properties to Disabled (/Od) ]
I appreciate any replies in advance.
so:
In main(...), the call to Handler with an argument will first call the
default ctor of the class which returns a temporary object and, then it
will call the copy ctor to copy the temp object to the object in the
Handler definition argument. So all in all, it calls two ctors, the
default and the copy ctor, respectively. Now, if this is true, can
someone explain why the default ctor only gets called in my compiler -
even though I've set optimizations to "Disabled (/Od)" in my VSC++ 2005
IDE? Could it be that this optimization is not an option to turn off or
on and that's why only the default ctor is called?
class Foo {
public:
Foo(const Foo& string) {
std::cout<<"copy-ctor"<<std::endl;
}
Foo() {
std::cout<<"def-ctor"<<std::endl;
}
};
void Handler(Foo string) {
}
int main(int argc, char** argv)
{
Handler(Foo());
return 0;
}
[p.s, I know the standard allows copy-ctors to be bypassed on unnamed
temp objects, which would explain my result but I've set optimizations
under properties to Disabled (/Od) ]
I appreciate any replies in advance.