What's the difference between lines 1 , 2 , 3 , 4?
When do I use each?
Why line 3 prints the constructor Foo
and line 7 returns an error and line 8 doesn't?
#include <iostream>
using namespace std;
class Foo
{
public:
Foo ( )
{
cout << "constructor Foo
";
}
};
class Bar
{
public:
Bar ( Foo )
{
cout << "constructor Bar
";
}
};
int main()
{
/* 1 */ Foo* foo1 = new Foo ();
/* 2 */ Foo* foo2 = new Foo;
/* 3 */ Foo foo3;
/* 4 */ Foo foo4 = Foo::Foo();
/* 5 */ Bar* bar1 = new Bar ( *new Foo() );
/* 6 */ Bar* bar2 = new Bar ( *new Foo );
/* 7 */ Bar* bar3 = new Bar ( Foo foo5 );
/* 8 */ Bar* bar3 = new Bar ( Foo::Foo() );
return 1;
}
See Question&Answers more detail:os