 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tracey Guest
|
Posted: Tue Apr 10, 2007 9:38 pm Post subject: std::<io> in main() |
|
|
Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
before prototypes), but [AFTER] the includes instead of [IN] main:
using std::cout;
using std::cin;
using std::endl;
The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
....
I'm trying to develop good habits for formatting source code.
It appears that includes go first...
then prototypes...
then main()...
then functions()...
Thanks, Tracey |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Tue Apr 10, 2007 11:26 pm Post subject: Re: std::<io> in main() |
|
|
"Tracey" <wpiis (AT) gte (DOT) net> writes:
| Quote: | Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
before prototypes), but [AFTER] the includes instead of [IN] main:
using std::cout;
using std::cin;
using std::endl;
The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...
|
The difference is just the scope of the using directive. At file
scope, everything in the file is "using" std::cout (and others), but
when it's in main, only main is using the name, and code outside of
main must still qualify the names.
You may think that it's better to use the names at file scope, because
more code (the whole file) can take advantage of it. Others might
argue that it's worse for the same reason.
The entire point of namespaces in C++ is to partition the names into a
hierarchy, to avoid collisions and conflicts. When you use a
namespace, or use names from a namespace, you're flattening the
hierarchy, and increasing the chance of there being a naming
collision. It is usually not a problem, but when it actually is a
problem, it can be incredibly subtle--such that sometimes you might
not even notice you have a problem, but worse, you have a problem and
don't know what it is.
I like to restrict the scope of using directives to the smallest scope
possible for them to still be valuable, if I use them at all. (It is
sometimes very convenient, but never necessary.)
--
Chris (TeamB); |
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Tue Apr 10, 2007 11:31 pm Post subject: Re: std::<io> in main() |
|
|
What is "better" is a value judgement. If asking five people, expect six
answers, not all in agreement.
If using an item inside a function and wishing to skip decorating it with
std:: then put the using statement inside the function, preferably at or
near the beginning of the function.
If using such an item throughout most functions in a source file then put
the using statement for that item after the includes and after any #pragma
hdrstop.
| Quote: | I'm trying to develop good habits for formatting source code.
It appears that includes go first...
then prototypes...
then main()...
then functions()...
|
I start with includes then functions. If there is to be one in this file, I
put main or WinMain last. I rarely use a prototype in the file where that
same function is located because I put a function in a source file before it
is used.
.. Ed
| Quote: | Tracey wrote in message
news:461baf89$1 (AT) newsgroups (DOT) borland.com...
Is it [better/best/irrelevant] to put the following [BEFORE] main
(and maybe before prototypes), but [AFTER] the includes
instead of [IN] main:
using std::cout;
using std::cin;
using std::endl;
The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...
I'm trying to develop good habits for formatting source code.
It appears that includes go first...
then prototypes...
then main()...
then functions()... |
|
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Tue Apr 10, 2007 11:41 pm Post subject: Re: std::<io> in main() |
|
|
Chris Uzdavinis (TeamB) <chris (AT) uzdavinis (DOT) com> writes:
| Quote: | "Tracey" <wpiis (AT) gte (DOT) net> writes:
Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
before prototypes), but [AFTER] the includes instead of [IN] main:
using std::cout;
using std::cin;
using std::endl;
The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...
The difference is just the scope of the using directive.
|
[picking nit] These are using *declarations*. |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Wed Apr 11, 2007 12:09 am Post subject: Re: std::<io> in main() |
|
|
maeder (AT) glue (DOT) ch (Thomas Maeder [TeamB]) writes:
| Quote: | The difference is just the scope of the using directive.
[picking nit] These are using *declarations*.
|
Thanks.
using std::cout; // declaration
using namespace std; // directive
--
Chris (TeamB); |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|