473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

anonymous derived class

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.

Apr 9 '07 #1
6 6963
Gaijinco wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 9 '07 #2
On Mon, 9 Apr 2007 08:23:02 -0400, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>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
Apr 9 '07 #3
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).
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.)

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

Apr 10 '07 #4
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. 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.
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.
Agreed here, and in the rest of your posting.

[...]

Apr 12 '07 #5
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

Apr 13 '07 #6
On Apr 13, 11:57 am, "James Kanze" <james.ka...@gmail.comwrote:
On Apr 12, 3:55 pm, "Stefan Schulz <schulz.ste...@gmail.com>"
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>')
test.cxx:8: error: non-local function '<anonymous classfunction()'
uses anonymous type
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.
Okay. I was unsure if the static storage or the limited namespace
aspect of the static keyword where required.

Apr 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
7
by: George Carl | last post by:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet I'd like to make publish/subscribe scenario where the publishing object is...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
3
by: Al Grant | last post by:
Consider two translation units, (1): namespace { struct S { }; } struct D: S { virtual int f(void); }; and (2): #include <typeinfo> struct D; char const *f(D *p) { return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.