James Kanze
11/24/2008 10:20:00 PM
On Nov 24, 9:20 pm, Noah Roberts <u...@example.net> wrote:
> Sam wrote:
> > doublemaster...@gmail.com writes:
> >> Is it possible to have class without name ?
> >> how to pass as argument to function ?
> > Can't. Well, you can always pass a void *. But, the function
> > won't have any idea what to do with it.
> class X { public: int f() { return 0; } };
> void function(X & x) { x.f(); }
> class : public X
> {
> } foo;
> int main()
> {
> function(foo);
> }
> Compiles and runs with g++.
Which reminds me of a real case of a class without a name which
I use in some cases:
class Callback
{
public:
virtual ~Callback() {}
virtual void visitOne( /* some arguments */ ) = 0 ;
} ;
void visit( Callback& action ) ;
and elsewhere:
void
f()
{
class : public Callback
{
public
virtual void visitOne( /* some arguments */ )
{
// whatever is needed...
}
} action ;
visitor( action ) ;
}
(Most of the time, I'll still give the class a name, however.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34