Why can I define structures and classes within a function in C++?
Solution 1:
[EDIT 18/4/2013]: Happily, the restriction mentioned below has been lifted in C++11, so locally defined classes are useful after all! Thanks to commenter bamboon.
The ability to define classes locally would make creating custom functors (classes with an operator()()
, e.g. comparison functions for passing to std::sort()
or "loop bodies" to be used with std::for_each()
) much more convenient.
Unfortunately, C++ forbids using locally-defined classes with templates, as they have no linkage. Since most applications of functors involve template types that are templated on the functor type, locally defined classes can't be used for this -- you must define them outside the function. :(
[EDIT 1/11/2009]
The relevant quote from the standard is:
14.3.1/2: .A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.
Solution 2:
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.
Solution 3:
It's actually very useful for doing some stack-based exception-safety work. Or general cleanup from a function with multiple return points. This is often called the RAII (resource acquisition is initialzation) idiom.
void function()
{
struct Cleaner
{
Cleaner()
{
// do some initialization code in here
// maybe start some transaction, or acquire a mutex or something
}
~Cleaner()
{
// do the associated cleanup
// (commit your transaction, release your mutex, etc.)
}
};
Cleaner cleaner;
// Now do something really dangerous
// But you know that even in the case of an uncaught exception,
// ~Cleaner will be called.
// Or alternatively, write some ill-advised code with multiple return points here.
// No matter where you return from the function ~Cleaner will be called.
}