Referring to the thread here: C++ object creation and constructor, the sentence Foo obj = Foo(1);
should involve two object creations: one invoking Foo(int)
, the other Foo(const Foo&)
, namely copy-ctor.
class Foo{
Foo(const Foo&){ //print "Foo(const Foo&) called" }
Foo(int i){//print "Foo(int) called" }
...
}
...
Foo obj = Foo(1);
The above code only prints "Foo(int) called". How does it work in this case? Since I add printing functions in the ctors, it shouldn't be optimized out by the compiler, right?