-
June 10th, 2009, 03:16 AM
#1
Inline Members
Sorry for such a simple question. I just want to clarify something.
If a method is defined inside the class declaration, it's automatically considered inline, correct? This would mean that I should avoid doing so except for very simple methods or constructors.
I'm pretty confident about this, but I'm looking at a large amount of code from a program here where all of the class methods are defined inside the class declaration and it didn't make sense to me. The best choice if you don't want to separate the implementation from the declaration would be to define the methods inside the header file, but outside the class declaration, right?
Thanks.
-
June 10th, 2009, 03:24 AM
#2
Re: Inline Members
Originally Posted by Raislin
If a method is defined inside the class declaration, it's automatically considered inline, correct?
Yes, though more precisely you mean class definition rather than class declarations in general.
Originally Posted by Raislin
This would mean that I should avoid doing so except for very simple methods or constructors.
Note that the compiler has the option of not inlining despite your suggestion that it do so.
-
June 10th, 2009, 03:44 AM
#3
Re: Inline Members
What's the difference between declaration and definition as far as classes go? I'm not sure I'm getting that.
-
June 10th, 2009, 03:46 AM
#4
Re: Inline Members
Originally Posted by Raislin
What's the difference between declaration and definition as far as classes go?
A definition is a declaration, but a declaration can also refer to a forward declaration such as:
-
June 10th, 2009, 04:27 AM
#5
Re: Inline Members
Look at example for function declaration and function definition.
Thanks for your help.
-
June 10th, 2009, 05:07 AM
#6
-
June 10th, 2009, 06:50 AM
#7
Re: Inline Members
Originally Posted by laserlight
A definition is a declaration, but a declaration can also refer to a forward declaration such as:
I always thought that the stuff in header file (class name, its members function etc) is a declaration whereas the definitions are the details of those declarations that go in .cpp file. Is it incorrect to think of it this way?
sgiri
-
June 10th, 2009, 09:11 AM
#8
Re: Inline Members
Originally Posted by sgiri1981
I always thought that the stuff in header file (class name, its members function etc) is a declaration whereas the definitions are the details of those declarations that go in .cpp file. Is it incorrect to think of it this way?
For functions, yes. For structs and classes, no.
EDIT:
Well, that probably deserves some elaboration.
In general, a declaration tells the compiler that something exists. You can then refer to what you have declared and the compiler accepts it, because you've told it that it exists.
A definition actually creates something (be it a variable, function, class, ...). It is the task of the linker to relate whatever you referred to in your code, to whatever you created.
E.g.
Code:
#include <iostream>
extern int n; // declaration of a variable
class X; // (forward) declaration of a class
void foo(X x); // function declaration
class X
{
public:
void bar()
{
std::cout << "X::bar()" << std::endl;
++n;
}
}; // class definition
int n = 0; // definition of variable
void foo(X x)
{
x.bar();
} // function definition
int main()
{
X x;
foo(x);
}
As you can see, I can use X and n before they are defined (but they are declared).
However, I cannot define foo() before I have defined X, because it uses a member function of X. If X is only declared, but not defined, the compiler doesn't know X::bar exists.
Last edited by D_Drmmr; June 10th, 2009 at 09:22 AM.
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 10th, 2009, 09:13 AM
#9
Re: Inline Members
Originally Posted by sgiri1981
I always thought that the stuff in header file (class name, its members function etc) is a declaration whereas the definitions are the details of those declarations that go in .cpp file. Is it incorrect to think of it this way?
sgiri
This is how I think of it:
Code:
// Declaration.
class Foo;
// Declaration.
class Foo
{
public:
void Func();
};
// Definition.
void Foo::Func()
{
}
// Declaration and definition.
class Foo
{
public:
void Func()
{
}
};
Last edited by Mybowlcut; June 10th, 2009 at 09:18 AM.
Reason: Wrong comment...
-
June 10th, 2009, 10:22 AM
#10
Re: Inline Members
Originally Posted by Mybowlcut
This is how I think of it:
Code:
// Declaration.
class Foo
{
public:
void Func();
};
That's not entirely correct. You are defining the class Foo, but only declaring it's member function Func().
It's easy to check, by the way. You can have multiple declarations of the same thing, but only one definition (if we consider only one compilation unit, at least).
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 10th, 2009, 10:35 AM
#11
Re: Inline Members
Originally Posted by D_Drmmr
That's not entirely correct. You are defining the class Foo, but only declaring it's member function Func().
I think that it can go either way... it is a declaration because you're only declaring the functions (interface), but it is also a definition because you are listing data members (implementation). I prefer to think of it as a declaration though, because the defining (function behaviour and data initialisation) is generally done elsewhere (unless I'm using templates in which case I tend to define everything at the point of declaration).
Originally Posted by D_Drmmr
It's easy to check, by the way. You can have multiple declarations of the same thing, but only one definition (if we consider only one compilation unit, at least).
Easy to check what? Multiple declarations of the same thing? You mean like multiple forward declarations?
-
June 10th, 2009, 12:04 PM
#12
Re: Inline Members
Originally Posted by Mybowlcut
I think that it can go either way... it is a declaration because you're only declaring the functions (interface), but it is also a definition because you are listing data members (implementation). I prefer to think of it as a declaration though, because the defining (function behaviour and data initialisation) is generally done elsewhere (unless I'm using templates in which case I tend to define everything at the point of declaration).
It is a class definition that contains a member function declaration. Because a class definition is also a class declaration, it is also a class declaration. If you think of it as a class declaration only, then it implies that you think that you cannot include the header that contains this class declaration and then instantiate a Foo object, because the Foo class has not yet been defined at that point.
Last edited by laserlight; June 10th, 2009 at 12:06 PM.
-
June 10th, 2009, 12:20 PM
#13
Re: Inline Members
Originally Posted by Mybowlcut
Easy to check what? Multiple declarations of the same thing? You mean like multiple forward declarations?
I mean this is possible:
Code:
class foo;
class foo;
Whereas this is not:
Code:
class foo
{};
class foo
{};
Hence, the class is defined twice in the last code, whereas it is declared twice in the first code. That's what I meant with 'checking'; you can use the compiler to check your understanding.
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 10th, 2009, 01:26 PM
#14
Re: Inline Members
I'm glad my topics generate some discussion. I had previously been interpreting it the same way Mybowlcut is, because, at a glance, that seemed to fit the way the words are used in other aspects of the language (functions, variables, structs, etc). However, I can completely understand why it's a function definition, thus also a function declaration.
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.
-
June 10th, 2009, 09:01 PM
#15
Re: Inline Members
Originally Posted by laserlight
It is a class definition that contains a member function declaration.
Ok, fair enough.
Originally Posted by laserlight
Because a class definition is also a class declaration, it is also a class definition.
Typo? Doesn't make sense to me though, even if I relate it to D_Drmmr's example; perhaps it's just the phrasing?
Originally Posted by laserlight
If you think of it as a class declaration only, then it implies that you think that you cannot include the header that contains this class declaration and then instantiate a Foo object, because the Foo class has not yet been defined at that point.
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? I think I see what you're saying, though, and it does make sense.
Originally Posted by D_Drmmr
I mean this is possible:
Code:
class foo;
class foo;
Whereas this is not:
Code:
class foo
{};
class foo
{};
Hence, the class is defined twice in the last code, whereas it is declared twice in the first code. That's what I meant with 'checking'; you can use the compiler to check your understanding.
Nothing like a good code sample... I get it now haha.
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
|