Re: What names can be used?
On Nov 1, 11:17 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Tadeusz Kopec" <tko...@NOSPAMPLEASElife.pl> wrote in message
news:472a4b50$1@news.home.net.pl...
Jim Langston wrote:
[snip]
Your problem may comein the fact that you're trying to
declare a variable with the same name as the class. I.E.
int main()
{
Ship Ship;
}
will not compile because they have the same name. One way people get
around
this is by using a small letter for isntances.
int main()
{
Ship ship;
}
which will work.
AFAIK declaring a variable of the same name, as a class has
is legal. It's sick for sure, but on my gcc 4.1.3 with
options -Wall -pedantic it compiled well. Probably it's for
backward compatibility with C.
O.o You're right, I just tested it. I don't know what to
say. I've just always known that it was illegal, not it turns
out it's legal.
Well, it would be illegal, except for reasons of C
compatibility. In C, struct names were in a completely separate
namespace, so no conflict was possible. C++ has a hack (and
there's no other word for it) to allow the traditional C usage:
if both a type name and a variable are in scope (which is the
case *after* your declaration, but not during it), then the name
resolves to the variable, except if it is preceded by class or
struct, e.g.:
int
main()
{
Ship Ship ; // legal...
f( Ship ) ; // refers to the variable...
struct Ship s ; // refers to the type...
}
It's probably better to just go on thinking that it's illegal.
Just remember that the compiler won't always complain, and may
do something unexpected, if you violate the rule.
(Also, for human readers, it's a good idea to distinguish
between types and non-types, since C++ parses differently
depending on whether a symbol is a type or not.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34