-
June 10th, 2009, 11:49 PM
#16
Re: Inline Members
Originally Posted by Mybowlcut
Typo? Doesn't make sense to me though, even if I relate it to D_Drmmr's example; perhaps it's just the phrasing?
Typographical error: the latter "definition" should be "declaration".
Originally Posted by Mybowlcut
I don't know enough about compilation etc. to really talk about stuff like this, but isn't the .cpp merged with the .h at compilation? E.g. the definition is always guaranteed to be complete by the time you include it (unless you have circular dependencies or haven't defined functions/statics/etc)? Hence making it a definition?
Well, that would be true if you were including the header in the source file that contains the member function definition. But what if you were including the header in another source file that contains code that is supposed to use objects of the class type?
-
June 11th, 2009, 05:43 AM
#17
Re: Inline Members
Originally Posted by laserlight
Well, that would be true if you were including the header in the source file that contains the member function definition. But what if you were including the header in another source file that contains code that is supposed to use objects of the class type?
I don't know haha. That's why I said I don't know much about it.
-
June 11th, 2009, 06:50 AM
#18
Re: Inline Members
This has been a great thought provoking thread, with the kind of discussions not found in any book.
Anyway, my chain of thoughts led me to wonder whats the difference between the following
Code:
#include <iostream>
#include "/relative path given in make file/MyProject/someFile.h"
In the first case, I believe the compiler will look for the standard C libraries provided along with the compiler (??? not sure here). Thats because we are enclosing the includes in < >.
Does it matter if these libraries are static or dynamic? Also, is there a separate file for each such library, or does it depend on the compiler (i use GCC)
Regarding second case, if some header file is not found in the path where it is supposed to be, then the compiler will complain. If the definition of a class is not found, then the linker will complain.
thanks in advance,
sgiri
sgiri
-
June 11th, 2009, 09:37 AM
#19
Re: Inline Members
Originally Posted by sgiri1981
In the first case, I believe the compiler will look for the standard C libraries provided along with the compiler (??? not sure here). Thats because we are enclosing the includes in < >.
I think that the text of the C++ standard should be a good read here:
A preprocessing directive of the form
Code:
# include <h-char-sequence> new-line
searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.
Originally Posted by sgiri1981
Does it matter if these libraries are static or dynamic?
This just concerns headers, so the type of linking is not relevant.
Originally Posted by sgiri1981
Also, is there a separate file for each such library, or does it depend on the compiler (i use GCC)
For the standard headers, it depends on the compiler. In theory, there might not even be a file for the header.
Originally Posted by sgiri1981
Regarding second case, if some header file is not found in the path where it is supposed to be, then the compiler will complain. If the definition of a class is not found, then the linker will complain.
Again, allow me to quote the standard:
A preprocessing directive of the form
Code:
# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
Code:
# include <h-char-sequence> new-line
with the identical contained sequence (including > characters, if any) from the original directive.
-
June 11th, 2009, 10:35 AM
#20
Re: Inline Members
Originally Posted by laserlight
I think that the text of the C++ standard should be a good read here:
This just concerns headers, so the type of linking is not relevant.
For the standard headers, it depends on the compiler. In theory, there might not even be a file for the header.
Again, allow me to quote the standard:
Thanks laserlight!
May I ask you the source of the c++ standard and is it readable for beginner/intermediate level programmer? (Google threw up some sites which were a little advanced for me)
sgiri
-
June 11th, 2009, 11:29 AM
#21
Re: Inline Members
Originally Posted by Raislin
What I decided to do about my situation is simple. For the code that I'm writing based off this book/source, I'm leaving the method definitions in the header file, but outside of the class definition. Makes me more comfortable, and seems cleaner to me. This way, you can quickly look through the class to see what's in it before looking at any method definitions.
Make sure you declare the function definitions as inline, else you run into linker errors when you include the header file in multiple cpp files.
Code:
class foo
{
void bar();
};
inline void foo::bar()
{
//...
}
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 11th, 2009, 04:07 PM
#22
Re: Inline Members
Originally Posted by D_Drmmr
Make sure you declare the function definitions as inline, else you run into linker errors when you include the header file in multiple cpp files.
Yeah, I already learned that the hard way. My intent was to try to keep from defining them as inline at all. But if I'm starting to understand correctly, that decision is made best by the compiler. I don't mind having a separate file for the definitions, though.
-
June 11th, 2009, 05:24 PM
#23
Re: Inline Members
Originally Posted by Raislin
What I decided to do about my situation is simple. For the code that I'm writing based off this book/source, I'm leaving the method definitions in the header file, but outside of the class definition. Makes me more comfortable, and seems cleaner to me. This way, you can quickly look through the class to see what's in it before looking at any method definitions.
That's not a problem for basic test/learning type programs, but don't get in habit for the long-term. When you start dealing with complex programs that involve dozens of classes, having everything in headers will mean that the slightest change in your source code requires huge amounts of recompiling.
-
June 11th, 2009, 06:15 PM
#24
Re: Inline Members
That's a good thing to remember. I intend to keep the definitions out of the header from now on anyway.
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
|