|
-
Mar 26th, 2003, 08:05 PM
#1
Thread Starter
yay gay
c++ code organization question
as i come from C# i noticed that we have function's prototypes in c++ in functions, and that in a class i put that and OUTSIDE the class is where i actually put the function's code......but isn't that a bit odd...ugly? how do u guys do? u put each class in a different file so no confusions are made or what? it seems to me a big mess up
\m/  \m/
-
Mar 27th, 2003, 09:27 AM
#2
Thread Starter
yay gay
for example..i see ppl making:
Code:
class TESTE {
public:
int public_int;
int get_public_int();
TESTE(int constructor);
};
TESTE::TESTE(int constructor) {
TESTE::public_int = constructor;
}
int TESTE::get_public_int() {
return TESTE::public_int;
}
although the following way works and for me (coming from C#) seems a lot better because the code gets better organizated
Code:
class TESTE {
public:
int public_int;
TESTE(int constructor) {
public_int = constructor;
}
int get_public_int() {
return public_int;
}
};
so..how do u do?
\m/  \m/
-
Mar 27th, 2003, 10:08 AM
#3
Frenzied Member
Yes I do it the same way as you do
But I can understand why people want to make only a prototype thing, so that someone who didn't write the class can still get a quick overview of what thing the class exposes and uses.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 27th, 2003, 10:43 AM
#4
Be aware that when you write the body of a function inside the class declaration it is automatically inlined by the compiler.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Mar 27th, 2003, 11:33 AM
#5
Frenzied Member
Lets say you have classes A and B. B uses A. A's source is some 3000 lines long (little extreme, but it makes the point). B is 30 or 40. A works perfectly, everytime, no bugs, however, B is giving you trouble, so you are debugging it.
Now, if A is written in your style, with the code inside the class declaration itself, the compiler has to compile A's working, 3000 line source file EVERY single time you compile B. This is going to take a LONG time. However, if you simply prototype A in a header file, and put the source in a source file, the source is compiled ONCE, and then linked each time you build your project.
Z.
-
Mar 27th, 2003, 12:24 PM
#6
Thread Starter
yay gay
so i should have 1file only with class prototypes and then each file the classes itselfs?
\m/  \m/
-
Mar 27th, 2003, 12:46 PM
#7
Frenzied Member
Originally posted by Zaei
Lets say you have classes A and B. B uses A. A's source is some 3000 lines long (little extreme, but it makes the point). B is 30 or 40. A works perfectly, everytime, no bugs, however, B is giving you trouble, so you are debugging it.
Now, if A is written in your style, with the code inside the class declaration itself, the compiler has to compile A's working, 3000 line source file EVERY single time you compile B. This is going to take a LONG time. However, if you simply prototype A in a header file, and put the source in a source file, the source is compiled ONCE, and then linked each time you build your project.
Z.
Ok didn't know that.. thanks alot, makes things clear
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 27th, 2003, 12:51 PM
#8
Lively Member
organization
I work this way. The class definition I put in a header file (extension .h) ... the definition of my class methods I put in a .cpp file. In this .cpp file you first include the library's you want to use like for example iostream, than you put #include "nameoftheclassdefinition.h" and than you can write the definition of your class methods.
-
Mar 27th, 2003, 03:24 PM
#9
Yet inlining or templating requires you to put function definitions in the header anyway.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|