[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

C++ Puzzle! Class without a name

doublemaster007@gmail.com

11/23/2008 2:59:00 PM

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 ?



.
4 Answers

Sam

11/23/2008 3:52:00 PM

0

doublemaster007@gmail.com writes:

> Is it possible to have class without name ?

Well, yes:

class {

public:
int foobar;

} foo, bar;


> if so, wat about the constructor and destructors ?

Can't have any.

> how to create a object of that class ?

Only together with the class declaration.

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

john

11/24/2008 8:20:00 PM

0

Sam wrote:
> doublemaster007@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++.

James Kanze

11/24/2008 10:20:00 PM

0

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

Gennaro Prota

11/25/2008 6:34:00 PM

0

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

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++)

--
Gennaro