Try to compile codes which demonstrate bolew
#include
<
iostream
>
class Bar {
public :
Bar() {}
};
class Foo {
public :
Foo( const Bar & b) {}
void blah() {
std::cout << " Foo::blah() called. " ;
}
};
int main()
{
Foo foo(Bar());
foo.blah();
}
class Bar {
public :
Bar() {}
};
class Foo {
public :
Foo( const Bar & b) {}
void blah() {
std::cout << " Foo::blah() called. " ;
}
};
int main()
{
Foo foo(Bar());
foo.blah();
}
You got an error, aren't you? What's wrong with the codes?
let's changed the codes to:
#include
<
iostream
>
class Bar {
public :
Bar() {}
};
class Foo {
public :
Foo( const Bar & b) {}
void blah() {
std::cout << " Foo::blah() called. " ;
}
};
Bar bar()
{
return Bar();
}
class Bar {
public :
Bar() {}
};
class Foo {
public :
Foo( const Bar & b) {}
void blah() {
std::cout << " Foo::blah() called. " ;
}
};
Bar bar()
{
return Bar();
}
int main()
{
Foo foo(Bar()); // this is just decclare an existence non-member function which param is another non-member functions and return value is Foo, and the Bar() part is the param of funtion fun and it returns Bar() and without any parameters
Foo x = foo(bar);
x.blah();
}
Foo foo(Bar(*pFunc)())
{
return Foo(pFunc());
}
{
return Foo(pFunc());
}
So the codes demonstrate why create temporaries using a default constructor can't be compiled.
And it will be ok, when you just initialize Foo using:
