Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variable names.

Reply
Thread Tools

variable names.

 
 
JustSomeGuy
Guest
Posts: n/a
 
      11-12-2003
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?


 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      11-12-2003
JustSomeGuy escribió:

> 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 and
> kept the rest of the variable names the same. Seems like a lot of work.
> 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.
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
how to implement variable ports with variable width? weijun VHDL 4 12-19-2005 05:16 PM
Variable 'variable lengths' Taras_96 VHDL 2 05-09-2005 12:58 AM
Convert Character Variable to Integer Variable Brad Smallridge VHDL 2 11-18-2004 01:56 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments