What does "class :" mean in C++?
It is an unnamed class, and the colon means it inherits privately from sample
. See it like
class Foo : private sample
{
// ...
};
Foo x;
I think that is defining an unnamed class deriving from sample
. And x
is a variable of that unnamed class.
struct sample{ int i;};
sample f()
{
struct : sample
{
// there were some members declared here
} x;
x.i = 10;
return x;
}
int main()
{
sample s = f();
cout << s.i << endl;
return 0;
}
Sample code at ideone : http://www.ideone.com/6Mj8x
PS: I changed class
to struct
for accessibility reason!
That's an unnamed class.
You can use them e.g. to substitute for local functions in pre-C++11:
int main() {
struct {
int operator() (int i) const {
return 42;
}
} nice;
nice(0xbeef);
}
The colon followed by sample
simply means derive from sample
using default inheritance. (for structs: public, for classes: private)