Topic: nameless classes or namespaces
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/03 Raw View
In article <user-0310990523060001@aus-as3-042.io.com>, blargg <postmast.
root.admi.gov@iname.com> writes
>In article <UxnAwCAxNS63Ew0f@robinton.demon.co.uk>, Francis Glassborow
><francisG@robinton.demon.co.uk> wrote:
>
>> In article <37E915F8.9FDBAEEB@wizard.net>, James Kuyper
>> <kuyper@wizard.net> writes
>> >Names are useful for linking two or more
>> >different uses of the named thing; if there's only one use, then the
>> >space used to store the name and the time spent typing and parsing it
>> >are all wasted. There's also the issue of name space pollution.
>>
>> Well chosen names document purpose.
>
>Then add a comment.
>
>A name indicates that it is used somewhere else. No name makes it clear
>that it isn't.
I think this is largely a style issue. I prefer a minimalist commenting
style so that every comment adds real value. Your choice is different.
However you better be very certain that you will never want to use it
elsewhere, because the cost will be adding a name and recompilation of
code.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/10/03 Raw View
Francis Glassborow wrote:
>
> In article <37E915F8.9FDBAEEB@wizard.net>, James Kuyper
> <kuyper@wizard.net> writes
> >Names are useful for linking two or more
> >different uses of the named thing; if there's only one use, then the
> >space used to store the name and the time spent typing and parsing it
> >are all wasted. There's also the issue of name space pollution.
>
> Well chosen names document purpose.
I just noticed that the reply I sent on 9/23 hasn't shown up yet. Here's
a repeat:
Yes, and in the cases where I use unnamed structs, the names that
document the purpose are the names of the objects or typedefs that I
define along with the struct. There's no possibility of leaving
undocumented the fact that these objects have something in common; since
the struct is unnamed, they must all be defined in the same declaration
that defines the struct.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: postmast.root.admi.gov@iname.com (blargg) (postmaster@nospam.gov)
Date: 1999/10/03 Raw View
In article <UxnAwCAxNS63Ew0f@robinton.demon.co.uk>, Francis Glassborow
<francisG@robinton.demon.co.uk> wrote:
> In article <37E915F8.9FDBAEEB@wizard.net>, James Kuyper
> <kuyper@wizard.net> writes
> >Names are useful for linking two or more
> >different uses of the named thing; if there's only one use, then the
> >space used to store the name and the time spent typing and parsing it
> >are all wasted. There's also the issue of name space pollution.
>
> Well chosen names document purpose.
Then add a comment.
A name indicates that it is used somewhere else. No name makes it clear
that it isn't.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: John_Maddock@compuserve.com (John Maddock)
Date: 1999/09/23 Raw View
>A class without a tag can't have a constructor except the the ones
>the compiler can generate. And unless you provide a typedef
> typedef class { ... } myclass;
>you can't refer to the class type later. Probably someone can
>invent a scenario using a tagless class, but it seems to me
>that tagless classes (as opposed to tagless unions) aren't very
>interesting.
There may be one possible use: to impliment the "empty member
optimisation":
template < class A>
class B
{
class : public A
{
public:
void* data;
}d;
public:
B() { d.data = 0; }
};
Still not very interesting though.....
John Maddock
http://ourworld.compuserve.com/homepages/John_Maddock/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/23 Raw View
In article <37E915F8.9FDBAEEB@wizard.net>, James Kuyper
<kuyper@wizard.net> writes
>Names are useful for linking two or more
>different uses of the named thing; if there's only one use, then the
>space used to store the name and the time spent typing and parsing it
>are all wasted. There's also the issue of name space pollution.
Well chosen names document purpose.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/22 Raw View
In article <37E8179A.7C57572D@wizard.net>, James Kuyper Jr.
<kuyper@wizard.net> writes
>I'll grant you that many features of C++ are unavailable to an unnamed
>class, but if you don't need any of those features, you get the same
>advantage you get in C: you don't have to think up a name for it. A
>small advantage, true.
Some of us might consider that to be a disadvantage: it makes the code
less readable and harder to understand.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/09/22 Raw View
Francis Glassborow wrote:
>
> In article <37E8179A.7C57572D@wizard.net>, James Kuyper Jr.
> <kuyper@wizard.net> writes
> >I'll grant you that many features of C++ are unavailable to an unnamed
> >class, but if you don't need any of those features, you get the same
> >advantage you get in C: you don't have to think up a name for it. A
> >small advantage, true.
>
> Some of us might consider that to be a disadvantage: it makes the code
> less readable and harder to understand.
Where does the unreadability of the code come in? If, as postulated,
I've got code that never needs to use the name of my class, why would it
reduce the readability of the code if the class didn't have a name? The
name would appear in precisely one location, the definition of the
class, and nowhere else. Names are useful for linking two or more
different uses of the named thing; if there's only one use, then the
space used to store the name and the time spent typing and parsing it
are all wasted. There's also the issue of name space pollution.
Of course, C++ rules make class names more useful than structure tags
are in C, so code which doesn't use them will be correspondingly less
common.
I could see some problems with the readability of error messages, rather
than of the code itself. But if you write perfect code, that won't
matter ;-)
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Mando Gali <M.Gali@deutschepost.de>
Date: 1999/09/21 Raw View
Hi,
can someone tell me in which situations a class or namespace without
identifier might be interesting?
thank you in advance
Mando Gali
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/09/21 Raw View
Mando Gali <M.Gali@deutschepost.de> writes:
>can someone tell me in which situations a class or namespace without
>identifier might be interesting?
>thank you in advance
A class without a tag can't have a constructor except the the ones
the compiler can generate. And unless you provide a typedef
typedef class { ... } myclass;
you can't refer to the class type later. Probably someone can
invent a scenario using a tagless class, but it seems to me
that tagless classes (as opposed to tagless unions) aren't very
interesting. The typedef example is common in C code, but writing
class myclass { ... };
is IMHO always preferable in C++.
An anonymous union -- a union definition without a tag, typedef,
or object -- brings the union members into the enclosing scope.
Objects can thus share storage without adding an access layer to
their names:
struct foo {
union { // anonymous union
int i;
double d;
myclass m;
yourclass y;
};
...
};
foo f;
f.d = 1.234; // member d is promoted directly to foo scope
OTOH, you often get better functionality and simpler code using
a class hierarchy and virtual functions. But if you want a "bag
of bits", this is one way to get it.
The unnamed namespace provides an alternative to static objects
and functions at file scope. The unnamed namespace in one
translation unit has, in effect, a secret name that is distinct
from the secret names of unnamed namespaces in other translation
units. Members of namespaces (including the unnamed namesapce) can
have external linkage and can thus be used as template arguments,
unlike static entities.
The unnamed namespace hides the names of types you want to use
throughout one translation unit but keep hidden from all other
translation units. You cannot get that exact effect any other way.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/22 Raw View
On 21 Sep 1999 17:38:17 GMT, Mando Gali <M.Gali@deutschepost.de> wrote:
>can someone tell me in which situations a class or namespace without
>identifier might be interesting?
Unnamed namespace -- good for helper functions in the cpp file.
--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/09/22 Raw View
Steve Clamage wrote:
....
> A class without a tag can't have a constructor except the the ones
> the compiler can generate. And unless you provide a typedef
> typedef class { ... } myclass;
> you can't refer to the class type later. Probably someone can
> invent a scenario using a tagless class, but it seems to me
> that tagless classes (as opposed to tagless unions) aren't very
> interesting. The typedef example is common in C code, but writing
> class myclass { ... };
> is IMHO always preferable in C++.
I'll grant you that many features of C++ are unavailable to an unnamed
class, but if you don't need any of those features, you get the same
advantage you get in C: you don't have to think up a name for it. A
small advantage, true.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]