I just mistakenly did something like this in C++, and it works. Why can I do this?
int main(int argc, char** argv) {
struct MyStruct
{
int some
One application of locally-defined C++ classes is in Factory design pattern:
// In some header
class Base
{
public:
virtual ~Base() {}
virtual void DoStuff() = 0;
};
Base* CreateBase( const Param& );
// in some .cpp file
Base* CreateBase( const Params& p )
{
struct Impl: Base
{
virtual void DoStuff() { ... }
};
...
return new Impl;
}
Though you can do the same with anonymous namespace.