Author Topic: Sugesstion about small changes in standard cpp code.  (Read 37184 times)

takeshimiya

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #15 on: September 12, 2005, 06:22:23 pm »
It's the same, but in most hello world examples, it's the using namespace statement, to show that feature of C++.

Also it's ok, because the scope is in the main.cpp, and probably other functions would be there, and will be using the std namespace also.

I'd suggest to put what Stroustrup or the books put as a 'hello world' example.

Offline me22

  • Official tester
  • Multiple posting newcomer
  • ***
  • Posts: 53
    • TA Universe
Re: Sugesstion about small changes in standard cpp code.
« Reply #16 on: December 06, 2005, 05:45:10 pm »
It's the same, but in most hello world examples, it's the using namespace statement, to show that feature of C++.

No, it's because most books about C++ are actually teaching you C ( just with cin/cout and some classes ) :x

using directives were introduced to aid in porting old code to the new standard, but none of the C++ experts ( Bjarne, Myers, Sutter, Andrescu, ... ) actually suggest using them.   using declarations are acceptable, providing their scope is limited.  ( Your main function could have using std::cout; using std::cin if all it does is deal with input and pass it off to other functions, for example. )

There's a whole lot of stuff in std:: ( which was a mistake, but that's how it is ) and you can really get bitten by it.  std headers are allowed to include whatever they want and iirc implementations are allowed to put whatever they want extra in std::, since the C++ user is technically not allowed to put anything in there.  This means that there can be all kinds of conflicting function and variable names.  Even the standard headers have functions with common names such as min, max, count, find, copy, ...
« Last Edit: December 06, 2005, 05:47:25 pm by me22 »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #17 on: December 06, 2005, 08:00:06 pm »
Hello,

I have found this article "C++ Namespaces and the 'using' Directive" interesting:

http://www.bgsu.edu/departments/compsci/docs/namespaces.html

Best wishes,
Michael

Ptomaine

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #18 on: December 09, 2005, 12:28:49 am »
What a wonderful thread I can see :)

"You can't imagine how it matters" (quot. from the "Charlie And The Chocolate Factory" movie)  :lol:

knue

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #19 on: December 09, 2005, 01:20:52 am »
Why not build a bug in the "Hello World" template?
All beginners wil despair. :D

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Sugesstion about small changes in standard cpp code.
« Reply #20 on: December 09, 2005, 04:53:27 am »
Actually, that's NOT a bad idea. If we put in the comments how to fix the lines, we might give beginners a crash course on bug fixing.

// My first bug (TM). This yields a "blablah" error because yadda yadda.
// To fix, replace the above line with:
// ...........

On a second thought, I can't imagine the dread WE will experience with "I fixed the hello world bug, but it still doesn't compile :( " complaints.  :eek:

No, thanks, nevermind.

takeshimiya

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #21 on: December 09, 2005, 09:18:24 am »
lol :D

How come a discussion of Hello World two pages long?

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #22 on: December 09, 2005, 10:44:02 am »
lol :D

How come a discussion of Hello World two pages long?

Yeah :D. But as big things born from smaller ones....

bszente

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #23 on: December 14, 2005, 08:07:03 pm »
How come a discussion of Hello World two pages long?
This thread is absolutely priceless  :lol:  Realy... I like it a lot.

I think the main should not contain the blocking statement. Anyway a console application should be run in a console, am I right? So actually Code::Blocks does his job well, when you run the console app from the IDE, Code::Blocks waits for e keystroke, when you run outside of the IDE you should run from a console. As simple as possible.

Concerning the main(), the parameters of the function should be definitely present:
Code
int main(int argc, char *argv[])
It's much more comprehensive.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #24 on: December 14, 2005, 08:26:23 pm »
Concerning the main(), the parameters of the function should be definitely present:
Code
int main(int argc, char *argv[])
It's much more comprehensive.

Personally, I prefer
Code
int main()
when I do not need specific options, otherwise
Code
int main(int argc, char *argv[])

What is important is that the main must return type int.

BS says in his technical FAQ:

Quote
A conforming implementation accepts
   int main() { /* ... */ }
and
   int main(int argc, char* argv[]) { /* ... */ }

Michael

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Sugesstion about small changes in standard cpp code.
« Reply #25 on: December 14, 2005, 08:29:52 pm »
Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #26 on: December 14, 2005, 08:38:43 pm »
Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.

When I was a C++ beginner, this main

Code
int main(int argc, char *argv[])

always scared me :D.

Now a little less, but still... :D.

Michael

Offline yop

  • Regular
  • ***
  • Posts: 387
Re: Sugesstion about small changes in standard cpp code.
« Reply #27 on: December 14, 2005, 09:53:27 pm »
Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.
A typical new project at my work:
Fire up KDevelop.
New QMake project -> Hello world application
Nope Hello world is not for newbies, it's the beginning of everything ;)
Life would be so much easier if we could just look at the source code.

bszente

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #28 on: December 15, 2005, 08:57:50 am »
Personally, I prefer
Code
int main()
when I do not need specific options

What is important is that the main must return type int.

Yes, you're right that only the int return type is important. However it's easier personally for me (and for the newbies) to delete the parameter list, than to re-type it (in case I need it). And for such console applications the command line parameters are frequently used...

What do you say about putting an "annoying" comment before the main... ? This way the newbies will not be scared, and this way they learn too...  :)
Code
// You may delete the parameter list, if you don't use it
int main(int argc, char *argv[])

For advanced users - remember: you always have the option to make your own template... I think those predefined hello world templates should target the newcomers. But of course all this subject is a matter of personal taste.
« Last Edit: December 15, 2005, 09:06:28 am by bszente »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #29 on: December 15, 2005, 11:38:32 am »
What do you say about putting an "annoying" comment before the main... ? This way the newbies will not be scared, and this way they learn too...  :)
Code
// You may delete the parameter list, if you don't use it
int main(int argc, char *argv[])

Yes, that could be a useful addition. May be:

Code
// If you do not use the parameter list, you can use instead:
// int main()
int main(int argc, char *argv[])

Important it is to not forget the returned type. I have read in an article that a teacher used:

Code
main()

Because it was easier to understand for the students. There was not the problem of returned type... :roll:. Na ja, could be. But this is not C++ or not ISO standard C++.

Michael