On Apr 12, 3:55 pm, "Stefan Schulz <schulz.ste...@gmail.com>"
<schulz.ste...@gmail.comwrote:
On Apr 10, 2:17 pm, "James Kanze" <james.ka...@gmail.comwrote:
On Apr 9, 6:57 am, "Gaijinco" <gaiji...@gmail.comwrote:
I have always felt that there are a lot of topics that you learned the
facts but you only grasp the matter sometime down the road.
For me, two of those topics are inner classes and anonymous classes.
Are you sure you're posting to the right group. C++ doesn't
have anonymous classes at all, and it usually uses the term
"nested class", rather than "inner class" (and nested classes in
C++ don't have quite the same semantics as inner classes do in
Java).
Hmm, i beg to differ:
class SomeBase {};
static class : public SomeBase {
int foo;
int bar;
} a;
is both legal, and does have a few limited uses.
Very few, although I've used it occasionally myself. I've never
heard this called an anonymous class, so it didn't occur to me,
but I guess the name fits. (The vocabulary used strongly
suggested Java, where "anonymous classes" and "inner classes"
are "features", described using just those words in the standard
literature.)
However, this is
limited to directly declaring _static_ instances,
Instances with static lifetime, you mean. Except that it's also
legal for auto variables. (And for return types, I think.)
How useful it is depends; I think it's probably most useful as a
local variable, in conjunction with the visitor pattern:
void
f()
{
struct : public Visitor
{
virtual void visit( T* obj )
{
// ...
}
} v ;
visitable( v ) ;
}
(Of course, if "visit()" is const, as it usually is, we
probably would prefer naming the class, and calling visitable
with a temporary.)
and can neither be
used with the new operator, or externally-visible objects.
It can be used with objects with external binding. The problem
is only that other translations units can't see the type. Even
in the case of something like:
extern struct
{
int a ;
int b ;
} toto
#ifdef DEFINITIONS
= { 42, -1 }
#endif
;
in a header, the type is formally a different type in each
translation unit.
I frankly
do not know how it interacts with namespaces.
Namespaces affect names. The type doesn't have a name, so it
doesn't interact:-). The name of the object obeys the same
rules as any name.
--
James Kanze (GABI Software) email:ja*********@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