anonymous derived class

G

Gaijinco

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.

I was thinking of a class Agenda. For it I would use a class Person
which also uses another class Date for her birthday.

When I was modeling Person, I made an atribute to be an object of
class Date. Suddenly I thought that maybe it was a good idea to made
it an inner classes. But then I supossed it would be even better to
make it an inner derived class. But to make it easier to acces members
of the inner class I made it annonymous.

Is this good practice? Does it makes sense? What good uses of inner
classes and anonymous classes do you know?

Thanks a lot.
 
V

Victor Bazarov

Gaijinco said:
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.

I was thinking of a class Agenda. For it I would use a class Person
which also uses another class Date for her birthday.

When I was modeling Person, I made an atribute to be an object of
class Date. Suddenly I thought that maybe it was a good idea to made
it an inner classes.

An inner class? Why? Are you talking about deriving from Date? Why?
What functionality can you possibly add to 'Date' that would warrant
inheriting? And I don't think you want to make such utility class as
'Date' inner to some other class...
But then I supossed it would be even better to
make it an inner derived class. But to make it easier to acces members
of the inner class I made it annonymous.

Could you please elaborate how having it anonymous makes it "easier"?
Is this good practice? Does it makes sense? What good uses of inner
classes and anonymous classes do you know?

I don't know any good use of anonymous classes. Anonymous unions have
their use, anonymous enums. But classes?

V
 
D

Dave Rahardja

I don't know any good use of anonymous classes. Anonymous unions have
their use, anonymous enums. But classes?

I can see anonymous classes as being anologous to anonymous (lambda)
functions.

-dr
 
J

James Kanze

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).
I was thinking of a class Agenda. For it I would use a class Person
which also uses another class Date for her birthday.
When I was modeling Person, I made an atribute to be an object of
class Date. Suddenly I thought that maybe it was a good idea to made
it an inner classes.

Why? Regardless of the language or the exact semantics of
inner/nested classes, I can't imagine ever making Date depend in
any way on Person.
But then I supossed it would be even better to
make it an inner derived class.

Why? And derived from what.
But to make it easier to acces members
of the inner class I made it annonymous.

Access what members? I'd say that there is a serious problem
with your design if Date has to start accessing members of
Person.
Is this good practice?
No.

Does it makes sense?
No.

What good uses of inner
classes and anonymous classes do you know?

I use nested classes in C++ when the class is an implementation
detail of the outer class, or an intrinsic part of the outer
class' interface. I use inner and anonymous classes in Java
when I want to confuse the reader, and make it more difficult to
understand my code. (Actually, I usually use them to work
around the lack of working multiple inheritance. But they're
very good for obfuscation as well.)
 
S

Stefan Schulz

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. However, this is
limited to directly declaring _static_ instances, and can neither be
used with the new operator, or externally-visible objects. I frankly
do not know how it interacts with namespaces.
Why? Regardless of the language or the exact semantics of
inner/nested classes, I can't imagine ever making Date depend in
any way on Person.

Agreed here, and in the rest of your posting.

[...]
 
J

James Kanze

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

Stefan Schulz

On Apr 12, 3:55 pm, "Stefan Schulz <[email protected]>"
Instances with static lifetime, you mean. Except that it's also
legal for auto variables. (And for return types, I think.)

I just checked it for return types, and it does not seem to be legal
for
return types.

At least g++ balks at the following "program"

class SomeBase {
};

class : public SomeBase{
public:
int xzs;
} function() {
}

int main(){
return 0;
}

test.cxx:8: error: new types may not be defined in a return type
test.cxx:8: note: (perhaps a semicolon is missing after the definition
of '<anonymous class>')
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.

Okay. I was unsure if the static storage or the limited namespace
aspect of the static keyword where required.
 

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

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top