By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

variable names.

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.