Topic: Different classes with the same name?
Author: Geert Denys <gdenys@yahoo.com>
Date: 09 Mar 01 00:08:30 GMT Raw View
Hi all,
When two classes exists in different translation units, at file scope
and with the same name, should they be considered one and the same?
The C++ standard seems to be unambiguous about this:
(section 3.5, basic.link)
<quote>
4. A name having namespace scope has external linkage if it is the
name of
...
- a named class, ...
9. Two names that are the same and that are declared in different
scopes shall denote the same ... type ... if
- both names have external linkage ...
<end quote>
Sure enough, this makes sense, since there's always at least a
constructor that's being generated for the class. Besides the
constructor, there's also the vtable, type_info, etc. But, maybe POD
is an exception to this?
However, In Stroustrup's "The Annotated C++ Reference Manual" (1990),
it seems possible that two different classes can exist with the same
name:
(section 3.3, Program and Linkage)
<quote>
In particular, since it is not possible to declare a class name
'static', every use of a particular file scope class name that has
been used in the declaration of an object or function with external
linkage or has a static member or a noninline member function refers
to the same class.
<end quote>
Here it's not explicitly excluded that two different classes can have
the same name.
Thanks!
Geert.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: ivec@mail.com (Ivan Vecerina)
Date: 9 Mar 2001 12:05:21 -0500 Raw View
Geert Denys said:
>When two classes exists in different translation units, at file scope
>and with the same name, should they be considered one and the same?
Yes. Namespace-scope class names must be unique, within a given
namespace, throughout the application.
This should not be a problem however, as:
- when a class is used within a unique translation unit, one should
enclose it in an anonymous namespace - which automatically creates
a unique scope: ( namespace { class MyClass {...}; } )
- for classes shared across translation units (basically all objects
declared in a .h file), one should use a module/library-specific
namespace name (which is an improvement over the cryptic prefixes
that are basically required for large scale C development).
>The C++ standard seems to be unambiguous about this:
>(section 3.5, basic.link)
Exactly. It can happen to work in some specific cases (e.g. when
no linkable objects are generated, or none happens to collide).
This more likely to be the case of PODs. But this is in no way
portable - the behavior is undefined.
Problems that can also occur with PODs include:
- exception handling if the POD is thrown
- name collisions in the mangled names of overloaded functions
- just anything an implementation might choose to do...
Regards,
Ivan
--
Ivan Vecerina - Surgical Navigation Network
--
Brainbench MVP for C++
http://www.brainbench.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://www.research.att.com/~austern/csc/faq.html ]
Author: "Conrad Weyns" <weyns@online.no>
Date: 9 Mar 2001 16:51:13 -0500 Raw View
"Geert Denys" <gdenys@yahoo.com> wrote in message
news:3AA77A5B.6BBF5911@yahoo.com...
> Hi all,
>
> When two classes exists in different translation units, at file scope
> and with the same name, should they be considered one and the same?
>
> The C++ standard seems to be unambiguous about this:
> (section 3.5, basic.link)
>
[]
> In particular, since it is not possible to declare a class name
> 'static', every use of a particular file scope class name that has
> been used in the declaration of an object or function with external
> linkage or has a static member or a noninline member function refers
> to the same class.
For what it's worth, the following works with both msvc and mwcw.
namespace {
class FileScope
{
public:
FileScope();
} _FileScope_;
FileScope::FileScope()
{
// put a break here to test it.
}
}
You can put one of these in any number of .cpp files.
I know, it's somewhat cheating, but it has a few far out uses.
I use something similar in a .h file that is included from many .cpp files
to allow me to check at run time the link order of translation units.
(Please let us not start a debate as to whether or not this should be
accepted by the purist Gods - I realy don't care!)
Conrad Weyns.
---
[ 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://www.research.att.com/~austern/csc/faq.html ]