variable names.



 DEVELOP > c-Plus-Plus > variable names.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "JustSomeGuy"
Date: 12 Nov 2003 01:25:32 PM
Object: variable names.
I just spent a few hours trying to find a bug in a friends code.
I finally found it, but I'm shocked to see what is wrong.
A private variable was defined in the class specification.
The constructor also had a variable of the same name defined.
We wondered why later on in the program things crashed. Well
its obvious now, but there were absolutly no complier warnings
about this variable. C++ doesn't seem to mind this and accepts it
as normal. I think it shouldn't..
Also when you have a constructor and the parameters to the constructor
just set member variables, how does one go about declaring the method
variables... We simple put underscores in the names of the variables and
kept the rest of the variable names the same. Seems like a lot of work.
Isn't there an easier way?
.

User: "=?iso-8859-1?Q?Juli=E1n?= Albo"

Title: Re: variable names. 12 Nov 2003 01:38:10 PM
JustSomeGuy escribi=F3:

We wondered why later on in the program things crashed. Well
its obvious now, but there were absolutly no complier warnings
about this variable. C++ doesn't seem to mind this and accepts it
as normal. I think it shouldn't..

Then don't use C++, use Ruby, for example ;)

Also when you have a constructor and the parameters to the constructor
just set member variables, how does one go about declaring the method
variables... We simple put underscores in the names of the variables an=

d

kept the rest of the variable names the same. Seems like a lot of work=

=2E

Isn't there an easier way?

You can use an initialsing list instead of assignments. That way the
parameter of the constructor and the member variable can use the same
name. But some people find this confusing and don't recommend it (the
reuse of the name, not the initialising list).
Here is an example:
class Point {
int x, y;
public:
Point (int x, int y) : x (x), y (y)
{ }
};
Regards.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER