I'm wondering if the following constructors are the same for C++:
class foo
{
public:
foo(void){};
...
}
and
class foo
{
public:
foo(void);
...
}
Do curly brackets matter for these two cases? Thanks much!
They're not same. {}
represents a regular function-body and makes the former function definition.
foo(void){}; // function definition
foo(void); // function declaration
Yes they do. The second one will generate undefined reference to foo::foo
(unless defined in another place). If you can use C++11 or above, you can use
foo()=default;
to define a compiler generated constructor
Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than the constructor would not already implicitly do.
In the second case, without them, the compiler will expect an implementation elsewhere - such as a .cpp file.
Yes. Without it is just a declarations. With both it is a declaration and definition. Try using it - you will get a linker error without the definition