News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

Sugesstion about small changes in standard cpp code.

Started by Angelo, September 11, 2005, 12:02:33 PM

Previous topic - Next topic

takeshimiya

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.

me22

Quote from: Takeshi Miya 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++.

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, ...

Michael

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
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

Ptomaine

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

Why not build a bug in the "Hello World" template?
All beginners wil despair. :D

rickg22

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

lol :D

How come a discussion of Hello World two pages long?

Michael

Quote from: Takeshi Miya on December 09, 2005, 09:18:24 AM
lol :D

How come a discussion of Hello World two pages long?

Yeah :D. But as big things born from smaller ones....
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

bszente

Quote from: Takeshi Miya on December 09, 2005, 09:18:24 AM
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:
int main(int argc, char *argv[])
It's much more comprehensive.

Michael

Quote from: bszente on December 14, 2005, 08:07:03 PM
Concerning the main(), the parameters of the function should be definitely present:
int main(int argc, char *argv[])
It's much more comprehensive.

Personally, I prefer int main() when I do not need specific options, otherwise 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
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

rickg22

Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.

Michael

Quote from: rickg22 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.

When I was a C++ beginner, this main


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


always scared me :D.

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

Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

yop

Quote from: rickg22 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.
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

#28
Quote from: Michael on December 14, 2005, 08:26:23 PM
Personally, I prefer 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...  :)

// 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.

Michael

Quote from: bszente on December 15, 2005, 08:57:50 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...  :)

// 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:


// 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:


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
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]