C++ Puzzle! Class without a name

D

doublemaster007

Is it possible to have class without name ?
if so, wat about the constructor and destructors ?
how to create a object of that class ?
how to pass as argument to function ?



..
 
N

Noah Roberts

Sam said:
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++.
 
J

James Kanze

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.)
 
G

Gennaro Prota

James said:
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 ; [...]
}

That's fine: action has no linkage. (I seriously hope for this
nonsense to go to the Google group someone has created for the
purpose. It's wasting everyone's time with incorrect answers to
incorrect questions. Most of the people involved should be
directed to alt.comp.lang.learn.c-c++)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top