Idiot's Question :\$

Recommended Posts

Hi there! I have a simple question. Which is the best and most used C++ coding-style? Can you give me some updated sites with those conventions? I need it because i'm having problems with that. For example.. I don't know which of this options is better: . function prototypes go first, then go with function main and after that put functions definitions . Function definitions go first and then function main. I know is a stupid question but I want to know which is the better coding style. I don't want to make mistakes now because then it's hard to fix them. Thank you guys, and good luck

Share on other sites
Well C++ is objected orinated, so you do things with classes. Each class is noramly made up of a header file and a source file. The class decliration goes in the header file and the source code to make things work go in the source file.

TheTroll

Share on other sites
Generally the first one, mostly because those functions usually aren't in the same file as main(), but are rather declared in header files included into the file which defines main(), and are themselves defined in different source files.

Share on other sites
Thank you guys! I hope I'm following the standard coding style.. i think this is like to playing guitar.. if you don't learn on the right way from the beginning.. you'll have to fix them later. More time pass.. more hard to fix them. (sorry.. my english is veeery poor.. :( )

Thank u again. Good bye!

Share on other sites
Don't worry too much about your English; you're doing fine. I've seen worse from people claiming to be native speakers.

Anyway, I recommend both:

- Within a source file, put the definitions in order so that things are defined before they're used (no prototypes). If there is some circular dependency (B calls A calls B), you will be forced to prototype one of them, and because you didn't just automatically prototype them all, you effectively get a warning about that dependency (the compiler brought it to your attention that A and B are calling each other, so you can look at how they do it and make sure it is OK). Plus you save the typing effort. Typically, the source file that serves as your "entry point" (the one that defines main()) will use this approach.

- Within a header file, put prototypes for functions that you want to "expose". Normally, for every *other* source file, you write a corresponding header file, and put prototypes for most (not necessarily all) functions of that source file into the header. Each source file includes its own header, as well as the headers for other source "modules" that it needs to know about. (Read this.) By *not* providing a prototype in the header, you make that function "private"; i.e. code in other source files can't make use of it. This is actually a good thing; you can use it to help make sure you don't do things by mistake that you didn't want to.

Anyway, since those source files need the prototypes anyway, you can then put the definitions in whatever order seems appropriate. Of course, those source files don't have their own main() anyway...

Share on other sites
If you meen something like this:

#include <iostream>using namespace std;void HelloWorld();int main(){         HelloWorld();         cin.get();}void HelloWorld(){         cout << "Hello World!\n";}

and this:

#include <iostream>using namespace std;void HelloWorld(){         cout << "Hello World!\n";}int main(){         HelloWorld();         cin.get();}

Then i cant see any difference in both. They both run at high speeds and i dont get any error messages. It all depends on your coding style. Try both and see which one you prefer.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628301
• Total Posts
2981913

• 10
• 11
• 11
• 10
• 10