What names can be used?

M

Michael Bell

I am learning C++ and I have just got onto classes.

I use Borland Builder 5

I was under the impression that you could give your variables any name
you liked, eg :-

NumbaOne
FatHarry
PagePeg

-: but I was astonished to discover, by trial and error, that if
you create a class like this :-

class ShipType
{
protected:
DeckType MethodName(void);
..
..
etc

then the ending "-Type" was compulsory, if you don't use the ending
-"Type", then it won't compile.

Is this really correct?

What other rules must you comply with?

Michael Bell

--
 
V

Victor Bazarov

Michael said:
I am learning C++ and I have just got onto classes.

I use Borland Builder 5

I was under the impression that you could give your variables any name
you liked, eg :-

NumbaOne
FatHarry
PagePeg

-: but I was astonished to discover, by trial and error, that if
you create a class like this :-

class ShipType
{
protected:
DeckType MethodName(void);
.
.
etc

then the ending "-Type" was compulsory, if you don't use the ending
-"Type", then it won't compile.
REALLY???

Is this really correct?

I am sure you are running into some other kind of error, but simply
misinterpret the cause.
What other rules must you comply with?

The FAQ server seems down (http://www.parashift.com/c++-faq-lite/),
but if you get to it, read 5.8.

V
 
G

Guest

I am learning C++ and I have just got onto classes.

I use Borland Builder 5

I was under the impression that you could give your variables any name
you liked, eg :-

NumbaOne
FatHarry
PagePeg

-: but I was astonished to discover, by trial and error, that if
you create a class like this :-

class ShipType
{
protected:
DeckType MethodName(void);
.
.
etc

then the ending "-Type" was compulsory, if you don't use the ending
-"Type", then it won't compile.

Is this really correct?

No, you can use about anything you like as an identifier as long as the
first character is a letter (note that the _ character is also
considered a letter but you should not use names that begins with an
underscore since they are in some situations reserved for the
implementation). There are also a number of special characters that can
not be used, but as long as you stick to letters and digits you should
be fine.
 
J

James Kanze

No, you can use about anything you like as an identifier as long as the
first character is a letter (note that the _ character is also
considered a letter but you should not use names that begins with an
underscore since they are in some situations reserved for the
implementation). There are also a number of special characters that can
not be used, but as long as you stick to letters and digits you should
be fine.

In fact, all special characters except _ are forbidden. The
matching regular expression is: [:alpha:_][:alnum:_]*.
 
J

Jim Langston

Michael Bell said:
I am learning C++ and I have just got onto classes.

I use Borland Builder 5

I was under the impression that you could give your variables any name
you liked, eg :-

NumbaOne
FatHarry
PagePeg

-: but I was astonished to discover, by trial and error, that if
you create a class like this :-

class ShipType
{
protected:
DeckType MethodName(void);
.
.
etc

then the ending "-Type" was compulsory, if you don't use the ending
-"Type", then it won't compile.

Is this really correct?

What other rules must you comply with?

No, this is not correct.

class Deck
{
};

class Ship
{
protected:
Deck Method() { return Deck(); }
};

should compile fine. 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. Please show us a small example of a program that will not
work without the "Type" suffix.
 
T

Tadeusz Kopec

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

Jim Langston

Tadeusz Kopec said:
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.
 
J

James Kanze

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

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top