473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

--
Oct 31 '07 #1
9 1450
Michael Bell wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '07 #2
On 2007-10-31 19:50, Michael Bell wrote:
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
Oct 31 '07 #3
* Victor Bazarov:
Michael Bell wrote:
>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
The FAQ's mirrored here: http://www.new-brunswick.net/workshop/c++/faq/

--
Derek
Nov 1 '07 #4
DerekBaker wrote:
[..]
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
Nov 1 '07 #5
On Oct 31, 11:02 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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:ja*********@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

Nov 1 '07 #6
"Michael Bell" <mi*****@beaverbell.co.ukwrote in message
news:0b********************@michael.beaverbell.co. uk...
>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.
Nov 1 '07 #7
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.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)

First rule of optimization: Don't do it.
Second rule of optimization (advanced): Don't do it yet.
Nov 1 '07 #8
"Tadeusz Kopec" <tk****@NOSPAMPLEASElife.plwrote in message
news:47********@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.
Nov 1 '07 #9
On Nov 1, 11:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Tadeusz Kopec" <tko...@NOSPAMPLEASElife.plwrote in message
news:47********@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:ja*********@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

Nov 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
10
by: Johnny Lee | last post by:
Hi, I'm new in python and I was wondering what's the difference between the two code section below: (I) class TestResult: _pass_ = "pass" _fail_ = "fail" _exception_ = "exception"
9
by: Jay | last post by:
Everywhere I go (read/browse) I see these parameters.... ByVal sender As Object, ByVal e As System.EventArgs Yet I never see them used within the function/method. Could someone tell me what they...
5
by: Ryan | last post by:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder Creek","STATE":"CA","DIST":5745.4} I get an "invalid label" error... I'm kinda new to this. Thanks!
5
by: Cylix | last post by:
this.menus = { root: new Array };
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.