[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: C++ template question.
From: Richard Smith <kde () metafoo ! co ! uk>
Date: 2005-02-28 21:40:12
Message-ID: 200502282140.13376.kde () metafoo ! co ! uk
[Download RAW message or body]
On Sunday 27 February 2005 21:52, Jonas Widarsson wrote:
> Can you confirm/deny if I got the following right please:
>
> * definition:
> class headers and function headers, just to let foreign code know what
> should be provided in other linked binary code. Definition also contains
> specialised types. Definition usually goes in files that end with .h
>
> * implementation:
> The inner workings of a class or function, written down i complete detail.
>
> * declaration:
> A small statement which assigns a variable name to a type, or creates an
> object on the heap?
Those are not accurate.
* declaration
A declaration tells the compiler that something exists, elsewhere. Examples:
void foo(); // foo is a function defined (implemented) somewhere else
extern int n; // n is an integer, probably defined in some other file
class ABC; // ABC is a class defined elsewhere
You usually put the declaration for every symbol (name) that you want other
files to be able to use in your .h file. This lets the compiler know that
those symbols exist, and tells it what sort of thing they are.
* definition
This is the thing that 'creates' the thing. Every definition is also a
declaration. Examples:
void foo() { abort(); }
int n;
class ABC
{
int foo;
public:
ABC();
};
ABC::ABC() { foo(); }
You usually would put the first two of the above in a .cpp file. The
definition of class ABC usually goes in a .h file, so code in other files can
use class ABC (with just a declaration of class ABC other code couldn't do
much more than pass around pointers and references to ABCs).
The definition of class ABC (above) contains a declaration of ABC's default
constructor, ABC::ABC(), but not its definition. Its definition is below the
definition of class ABC. You would usually put the definition of a member
function in a .cpp file. The definition of class ABC also contains the
definition of ABC's member variable, foo.
Notice how I've been saying "a declaration" but "the definition"? That's
because you're allowed any number of (consistent) declarations of a symbol,
but you're only allowed one definition (though what "one definition" means
varies).
* implementation
This really just means "the way that something is done". So if you talk about
the implementation of a function, you're talking about the sequence of
statements in its body, and if you talk about the implementation of a class
you're talking about its data members, how its member functions work, how it
interacts with other classes. On a larger scale, you can talk about the
implementation of a library, and mean how it does whatever it is that it
does.
So, the usual solution to your problem is to put the definition for your
templated member function (including the code inside fillBuffer() - its
implementation) into the .h file. I hope you followed all that.
> BTW, you use the keyword "struct" instead of "class". Why? I thought struct
> only applies to data and not to executable code.
struct and class are very nearly identical from a compiler's point of view.
The only difference is that in a struct, the default visibility is 'public'
and in a class the default visibility is 'private'. So the following two code
snippets have the same meaning to a compiler:
struct Foo : Bar
{
int n;
};
class Foo : public Bar
{
public:
int n;
};
To a C++ programmer, a 'struct' will usually mean a collection of data,
possibly with some member functions, usually with no invariants of its own,
and a 'class' will usually mean an encapsulated object, with invariants
(like, "n is less than 10") and methods which maintain those invariants
(like, "increment n if it's less than 10").
--
Thanks,
Richard
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic