First, S s();
declares a function named s
that takes no arguments and returns an object of type S
. Just like int f();
.
Second, that extern "C"
isn't relevant here. It's used, roughly, for functions that are written in C and called from C++. It doesn't mean "pretend that this code is C code".
Third, S
does have a default constructor. The compiler generates one. In this case it doesn't do anything, because there are no members of S
that require non-trivial initialization. To use the default constructor you'd write S s;
or S s{};
.
Fourth, the reason that declaring a member function is okay is that a struct
and a class
in C++ can have member functions. I know, that sounds tautologous, but it's important to keep in mind that a struct
can have member functions, static data, private
, protected
, and public
members just like a class
. The only differences between a struct
and a class
is that by default members of a class are private while members of a struct are public, and by default a base of a class is inherited privately while a base of a struct is inherited publicly.