_ in variable name

Aug 9, 2011 at 9:11pm
hi,
Is code written like this supposed to go into a class definition vs. in main()
1
2
3
4
5
6
7
8
if(_infile.is_open())
  {
  _infile.close();
  _linenum=0;
  }
 else std::cout<<"unable to open file!"<<std::endl;

getline(_infile,_line);

Does the _ imply that these variables are private?
I included it in main() and I get errors:
 
error: ‘_line’ was not declared in this scope

Aug 9, 2011 at 9:21pm
Does your IDE support "go to definition"? If so, see if you can find who's defined it.

Andy

P.S. My answer makes no sense, now I've actually read the org. post...
Last edited on Aug 10, 2011 at 12:21am
Aug 9, 2011 at 9:24pm
You should not start any identifier with an underscore. That is reserved for compiler or environment-specific keywords.
Aug 9, 2011 at 9:33pm
I am working with emacs on ubuntu.
I guess if I am not able to figure out who defined it, and given that I shouldn't be using the _, do I have no choice but to rewrite somebody else's code?
Aug 10, 2011 at 12:20am
Sorry, I misread your message. (It's a missing variable, not a variable collision; I assumed the latter due to the _ prefix)

If the code you inherited was supposed to be complete, it could just be a missing header. If not, you have no choice but to add the missing code.

The _prefix is used by some people for member variables. By some for other variables. But, as Disch said, it's best to avoid variable like that.

I assume you've had a good grep!

Andy
Topic archived. No new replies allowed.