Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 31st, 2007, 08:35 PM
Michael Bell
Guest
 
Posts: n/a
Default What names can be used?

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

--
  #2  
Old October 31st, 2007, 08:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: What names can be used?

Michael Bell wrote:
Quote:
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???
Quote:
Is this really correct?
I am sure you are running into some other kind of error, but simply
misinterpret the cause.
Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old October 31st, 2007, 11:05 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: What names can be used?

On 2007-10-31 19:50, Michael Bell wrote:
Quote:
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.

--
Erik Wikström
  #4  
Old November 1st, 2007, 03:45 AM
DerekBaker
Guest
 
Posts: n/a
Default Re: What names can be used?

* Victor Bazarov:
Quote:
Michael Bell wrote:
Quote:
>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???
>
Quote:
>Is this really correct?
>
I am sure you are running into some other kind of error, but simply
misinterpret the cause.
>
Quote:
>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
The FAQ's mirrored here: http://www.new-brunswick.net/workshop/c++/faq/

--
Derek
  #5  
Old November 1st, 2007, 04:45 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: What names can be used?

DerekBaker wrote:
Quote:
[..]
The FAQ's mirrored here:
http://www.new-brunswick.net/workshop/c++/faq/
Thanks! I've bookmarked this one.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #6  
Old November 1st, 2007, 11:45 AM
James Kanze
Guest
 
Posts: n/a
Default Re: What names can be used?

On Oct 31, 11:02 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
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:_]*.

--
James Kanze (GABI Software) email:james.kanze@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

  #7  
Old November 1st, 2007, 09:45 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: What names can be used?

"Michael Bell" <michael@beaverbell.co.ukwrote in message
news:0b098a3a4f.michaelbell@michael.beaverbell.co. uk...
Quote:
>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.


  #8  
Old November 1st, 2007, 11:05 PM
Tadeusz Kopec
Guest
 
Posts: n/a
Default Re: What names can be used?

Jim Langston wrote:
[snip]
Quote:
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.

--
Tadeusz B. Kopec (tkopec@NOSPAMPLEASElife.pl)

First rule of optimization: Don't do it.
Second rule of optimization (advanced): Don't do it yet.
  #9  
Old November 1st, 2007, 11:25 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: What names can be used?

"Tadeusz Kopec" <tkopec@NOSPAMPLEASElife.plwrote in message
news:472a4b50$1@news.home.net.pl...
Quote:
Jim Langston wrote:
[snip]
Quote:
>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.


  #10  
Old November 2nd, 2007, 12:05 PM
James Kanze
Guest
 
Posts: n/a
Default Re: What names can be used?

On Nov 1, 11:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
Quote:
"Tadeusz Kopec" <tko...@NOSPAMPLEASElife.plwrote in message
news:472a4b50$1@news.home.net.pl...
Quote:
Quote:
Jim Langston wrote:
[snip]
Quote:
Your problem may comein the fact that you're trying to
declare a variable with the same name as the class. I.E.
Quote:
Quote:
Quote:
int main()
{
Ship Ship;
}
Quote:
Quote:
Quote:
will not compile because they have the same name. One way people get
around
this is by using a small letter for isntances.
Quote:
Quote:
Quote:
int main()
{
Ship ship;
}
Quote:
Quote:
Quote:
which will work.
Quote:
Quote:
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.
Quote:
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ée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles