Most people say global variables are bad, (true) but my book says cout<<"" and cin>> are global variables.
Most people say global variables are bad, (true) but my book says cout<<"" and cin>> are global variables.
Stupid things pop singers say
"I get to go to lots of overseas places, like Canada."
- Britney Spears
"I love what you've done with the place!"
-Jessica Simpson upon meeting the Secretary of Interior during tour of the White House
And eventually you realize C++ is a huge mess .
They also say that you shouldn't make operators do things that aren't related to how the operator works with native types, but what does outputting and inputting data (<< and >>) have to do with left and right bitshifts?
A global variable that serves many programs could hardly be called bad. What is bad is overusing or misusing them, usually in a single program.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
adding to what Sebastiani said...
global variables are not recommended because it is a hard error to debug if an error arises. global constants are fine though, because the program can't change to value.
for instance, if a function in a program changes the value and it isn't supposed to, then this error is usually difficult to find and correct. however, if it is a global constant, the value cannot be changed, and therefore, reduces the possibility of error.
Yes they're global variables, but cin and cout are part of the standard libraries which means they were written by people much smarter and better at programming than you or I (or at least, I hope that's true). Really, cin and cout are more of the exception, not the rule. My programming teacher said that in all of his years of programming, only once has he used a global variable, and that's because there was no other way he could do it.Originally posted by fuh
Most people say global variables are bad, (true) but my book says cout<<"" and cin>> are global variables.
In short, don't use global variables unless there is absolutely no other way to do it.
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
There are always other waysOriginally posted by joshdick
only once has he used a global variable, and that's because there was no other way he could do it.
I'd say that cout and cin are global variables mostly because of ease of use. I would prefer if they weren't globals, but in this case it really doesn't matter much. As joshdick said, the fact that it's in the standard libraries guarantees that it's properly debugged so that you don't have to worry about it much. It's just something you learn to accept. Global variables are always discouraged, but as long as you're smart about it you can limit the problems they cause. Still, don't use them... ever.
Hey, man, I don't know the particulars of my teacher's situation. You'll have to take that up with him.Originally posted by Polymorphic OOP
There are always other ways
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
Hmm... I dont like to think of them as global variables. I like to think of them as pre-defined functions that're lots of fun to use.Originally posted by fuh
but my book says cout<<"" and cin>> are global variables.
- Dean
Nope, they're global objects not functions. Well cout and cin are. << and >> are just functions.Originally posted by LordVirusXXP
Hmm... I dont like to think of them as global variables. I like to think of them as pre-defined functions that're lots of fun to use.
Then again most of programming is about solving problems, not theoretical best solutions. I've opted for global variables in situations where other methods would have worked but globals were the quickest and easiest. I'm comfortable enough with programming now that I feel I can make these decisions; as opposed to blindly listening to what-to-do and what-not-to-do.
Admit it, not every component of every program you've make is a elegant masterpiece-- if it is, i'd bet you haven't done much.
void main(), global variables, goto... they all have a place in my universe.
see if you can spot the joke
That's pretty arrogant. Not every program has to be an elegant masterpiece to avoid using global variables. Though I do agree that it's not a great idea to just blindly accept "don't ever use them." It's really something you have to work with to understand.Originally posted by Eibro
Admit it, not every component of every program you've make is a elegant masterpiece-- if it is, i'd bet you haven't done much.
I hope that "void main()" was that joke you were referring to ...Originally posted by Eibro
void main(), global variables, goto... they all have a place in my universe.
Oh screw you guys, cin and cout are not global. They're only global in namespace std!
Ha, yeah, I always laugh when people use that excuse -- as if namespaces remedy the problemOriginally posted by Eibro
Oh screw you guys, cin and cout are not global. They're only global in namespace std!
Yeah, that was pretty much my point. Plus, global variables are only one tip of the iceburg.It's really something you have to work with to understand.
What?I hope that "void main()" was that joke you were referring to ...
I was refering to what you wrote in your earlier reply in white -- "see if you can spot the joke"Originally posted by Eibro
What?
And I said that I hoped you were referring to void main() because if you weren't then you do realize that I would have to kill you