Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Oluseyi

Advanced: Anonymous classes (C++)?

This topic is 5830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone shed light on the uses of anonymous classes in C++? I''ve discovered that compilers will internally identify/name-mangle the class(es) (generally, variations on CRC file #s and Perl-like $_ constants), so the functionality is obviously supported. When would it be useful and what common (ha!) or popular applications exist? [Note: If you don''t know, don''t speculate. Just zip it and keep moving. Thank you.]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You can do this in Java, but C++ doesn''t support anonymous classes. In Java anonymous classes are useful for giving class definitions that respond to events in place, rather than having the definition elsewhere. It''s also useful for providing predicates in the form of functors (functions wrapped in a struct/class) in place as opposed to putting the definition elsewhere. I like this because when the code is elsewhere it disassociates the functionality from where it is actually used. Anonymous classes solve this problem.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can use anonymous structs(I presume classes can be used in the same way)as members of a union.

For example:
typedef union {
struct {int X; int Y};
struct {char A; char B};
int Z;
} XandYorAandBorZ;

Thus, x and y would be the members of the union, or a and b would be the (entire) contents of the union, or z by itself could be the contents of the union.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
You can use anonymous structs(I presume classes can be used in the same way)as members of a union.



This is not standard. Anonymous nested union, yes. Anonymous nested struct, no.

Oluseyi:

I assume you mean class { /*...*/ } foo;, just like you may do struct { /*...*/ } bar;. Since the only difference between class and struct are the default access permissions, I can only imagine it would be used in the same cases : as a singleton of sorts (without a class name, you can't have a custom constructor or destructor, nor create new instances), whether at global scope, or nested within another class.

I have seen unnamed structs used mainly as global tables of some kind : struct { /*...*/ } const foo[] = { /* ... */ };. Using class instead of struct makes little difference, especially when you consider that class : public foo { /*...*/ } bar; gives parse errors.

I hope I wasn't too speculative for you

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 30, 2002 7:14:37 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
You can do this in Java, but C++ doesn''t support anonymous classes. In Java anonymous classes are useful for giving class definitions that respond to events in place, rather than having the definition elsewhere.

Interesting. As Fruny pointed out, though, C++ supports anonymous classes/structs for instantiation of a single object or set of objects declared and defined along with the class (class {/*...*/} x; variety). Could you give an example of an anonymous class in Java that responds to events in-place?

quote:

It''s also useful for providing predicates in the form of functors (functions wrapped in a struct/class) in place as opposed to putting the definition elsewhere. I like this because when the code is elsewhere it disassociates the functionality from where it is actually used. Anonymous classes solve this problem.

C++ supports nested classes as well as local classes, which can be very useful since it doesn''t permit local functions. If you wish to provide, say, a predicate function that is only ever used within the current scope, wrap said function as a functor:

int SomeFunction( int x )
{
struct Pred
{
bool operator () ( int & i)
{/* yadda */}
} pred;
 
some_stl_algorithm( some_container.begin(), some_container.end(),
pred );
}

Obviously, combining this example with earlier comments on immediate instantiation, it should be possible to omit the class identifier Pred in the code above.

Thanks AP and Fruny. And no, you weren''t speculative by a long shot; you merely both reminded me of language features moved to the back of my mind.

Share this post


Link to post
Share on other sites
I tried without the name in VC7, I got a 'C2627' - member function defined in unnamed class. Interestingly, the symbol name for the named class was ::main::Foo.

I vaguely remember some design difference between C and Pascal where Pascal can have local functions, but C can have static variables... (some dynamic scoping and lifetime issues IIRC)

If somebody could enlighten me...

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 30, 2002 11:48:00 PM]

Share this post


Link to post
Share on other sites
anonymous classes are used extensively in java in UI programming. for example, the following code adds program termination capability to a Button reference.


      
// anonymous class in the function call.

quitButton.addActionListener( new ActionListener( ){

// in place event response

public void actionPerformed( ActionEvent e ){
terminate( );

}
});


honestly, I am not well versed enough in C++ to respond about such things in it.


[edited by - lucinpub on July 1, 2002 2:40:14 AM]

Share this post


Link to post
Share on other sites
Every GUI component in Java should ideally have its own *Listener. Imagine if you had 20 buttons on screen, w/o anonymous innerclasses, you'd have to create 20 different classes that implemented an ActionListener interface.

Anonymous inner classes can be used to simulate multiple inheritance in Java. For a better example, check out the chapter on inner classes in the book Thinking in Java.

EDIT: p.s.: In Java, every use of an anonymous inner class can be done w/o using anonymous inner classes. I think it boils down to a matter of style.



[edited by - NuffSaid on July 1, 2002 8:03:45 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
C++ supports nested classes as well as local classes, which can be very useful since it doesn''t permit local functions. If you wish to provide, say, a predicate function that is only ever used within the current scope, wrap said function as a functor

Not on MSVC. Don''t know if this is a compiler shortcoming, but it won''t allow you to instantiate a template from a function-local class definition, which means no functors for STL algorithms. If anybody knows a way around this, I''ll name my first-born after you.

Share this post


Link to post
Share on other sites
Stoffel:
Good point. I remember struggling with the same issue the first time I learnt about the local class functor method from GoTW. No cigar.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!