I’ve just come across the following code:
#include <iostream>
static class Foo
{
public:
Foo()
{
std::cout << "HELLO" << std::endl;
}
void foo()
{
std::cout << "in foo" << std::endl;
}
}
blah;
int main()
{
std::cout << "exiting" << std::endl;
blah.foo();
return 0;
}
I haven’t seen the above method of definining a variable before – the class definition is done inline with the variable definition. It reminds me of anonymous classes in Java. What is this called, and is it in the C++ standard?
Thanks
Taras
It’s quite standard to define a
class(orstruct, perfectly equivalent except that the default ispublicinstead ofprivate) and declare a variable of its type (or pointer to such a variable, etc) — it was OK in C (withstruct, but as I already mentioned C++’sclass, save for public vs private, is the same thing asstruct) and C++ mostly maintains upwards compatibility with (ISO-1989) C. Never heard it called by any special name.