Why just use prototypes with classes?

This is a discussion on Why just use prototypes with classes? within the C++ Programming forums, part of the General Programming Boards category; When you write a class function, why is it bad form to make it anything other than a prototype within ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question Why just use prototypes with classes?

    When you write a class function, why is it bad form to make it anything other than a prototype within the class? In other words, why is it usually good form to define the function outside of the class definition?

    The way I understood it, classes were supposed to be portable lines of programming that somewhat resembling real-life objects, actions, and so on (or fantasitical stuff, but symbolic as opposed to procedural). If this is so, then would it not be a waste of time to write a class full of defaultly useless variables and functions, then in the less portable areas of the program, finally write the data that makes the class useful?

    When I program, I don't need somebody else helping me name my own variables and functions. But what I would possibly need from somebody else would be code that actually does stuff. Function libraries, for example.

    I understand that even if a class variable or function is defined elsewhere in the program, it will be completely hand-in-hand with that class, and the classes created on one particular program might help speed up the programming process in that specific case. But when they are ported to another program, as I understand they oftentimes are, why not put the variable and function definitions inside the class to actually make it useful?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > When you write a class function, why is it bad form to make it anything other than a prototype within the class? In other words, why is it usually good form to define the function outside of the class definition?

    Keep in mind, functions defined inside class definitions are marked as inline by the compiler. http://www.parashift.com/c++-faq-lit...s.html#faq-9.8

    I suppose it might lead to excessively large executables, and it would definitely be messier code to read.

    I can't make sense of the rest of your post.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Your post was very hard to understand but I think I understood it. As for your first question, it's better to split them up if you do not wish the functions to be inlined because it makes the code easier to read. For example, if you simply want to check what are the methods of a class, you don't want to scroll through all of the implementation details, you want to have a look at how the class is designed. As for your second question, well it's not that hard to include a few files in your project to re-use a class.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    I probably cannot come with all the answers. But maybe the following will lead you somewhere. Others may be more incisive tha I will.

    First and foremost you have to consider code readability. This should always be your major concern whether you are designing a class or deep inside main(). A class header file that shows itself only as a series of data members and function prototypes is a much easier class to understand, both by you that wrote it and to anyone else. Even simple class definitions that return or set data member values will distract the reader from the class structure that is usually what one wants to see when looking at it from an advantage point. Mainly what you want is to give the class user a bird's eye view of your class.

    But there is more to it. Any function defined inside a class definition is by default a candidate for inlining. While it is true the compiler will do that choice for you, it is nonetheless a fact that you may not always want inlining to occur. At the very least you may want to have a say on this matter, even if the compiler sees that function as a good candidate. And this is so because of http://www.parashift.com/c++-faq-lit...s.html#faq-9.3

    And there is also more to it. Some functions, depending on how your class is constructed, will only really do their thing if the class is fully defined. This may be so, because they need to access other member functions that have yet not been declared, for instance. You will find yourself in a bind if you have two functions referring to each other somewhere inisde their definition. You will end up having to prototype one of them, needlessly complicating your class definition to a point it simple becomes unredable and unmaintanable.

    And finally... the least important of the reasons, but still a valid one. While separating the "interface from the implementation" paradigm of class development should be seen as a code technique, not as a data hiding feature, it is no less true that we should try and mimic it to some extent in the way we code our class. At least, to make the interface clear to the class user. This argument could be seen as a repeat of the first. But the truth is that we realy don't want to hide the implementation from the class user. It can be useful to them to learn how the class implements its features. We simply want the logical separation between interface and implementation to be more obvious to the human brain.

    I never define any function inside the class defintion as a norm. I reserve that only to very small classes or structs. Even my inlined functions are defined inside the header file after the class definition. You don't need to go to that extreme (although you will not see me looking at this as an "extreme" approach). But you should look at class defintions the same way you look at any other header file and that is:

    - header files contain declarations, const definitions and inline function defintions
    - cpp files contains the defintions.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    MS "Visual Studio form designer" always defines objects inside the form class.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Keeping code in the .h files increases compile times. Everytime you change the code, every file that includes that .h file needs to be recompiled. If you have a large project, that can mean hours vs a few seconds if it were in the .cpp.

    I think the second point of your post summarizes as "isn't it better to have one file that you can reuse on other projects than needing to worry about two files?" If that is what you were trying to say, then don't worry, you'll get used to keeping .h and .cpp files paired up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21