Results 1 to 9 of 9

Thread: c++ code organization question

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    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/

  2. #2

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    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.

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    so i should have 1file only with class prototypes and then each file the classes itselfs?
    \m/\m/

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  8. #8
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64

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

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width