8

I am reading programming principles and practice using c++ (Bjarne Stroustrup),

9.4.4 Defining member functions ,page 312, it says

1.

writing the definition of a member function within the class definition has two effects...

2.

Don't put member function bodies in the class declaration unless you know that ...

does the author write wrong? it talks about the same thing, why first sentence is "class definition" and second is "class declaration" ?

thanks

booirror
  • 332
  • 3
  • 8
  • What defects does the author talk about with defining member functions within the class definition? FYI, doing so is necessary for class templates. – Praetorian Jun 02 '13 at 14:01
  • Yeah… jargon quirks aside, it sounds like bad advice. There's nothing wrong with defining a function inside the `class {}` block; it's usually the clearest way to implement an `inline` function. For the (uncommon) case of a non-template friend of a class template, you *must* define it in that scope. – Potatoswatter Jun 02 '13 at 14:35
  • @Praetorian - defining member functions within the class definition is **not** necessary for class templates. It's a common style, and makes the code look a great deal like Java. But in general there's nothing that prevents defining member functions outside the template definition with properly qualified names. – Pete Becker Jun 02 '13 at 18:36
  • @PeteBecker True; but I was referring to separating the definition from the declaration in source and header files, which you cannot do in case of class templates (unless you also `#include` the source file). – Praetorian Jun 02 '13 at 23:21

3 Answers3

10

does the author write wrong?

Not wrong, but imprecise. A class declaration can be a forward declaration, such as:

class X;

This just makes the compiler aware of the existence of that class, but does not specify what members the class has, what base classes, and so on (that's what a class definition does).

However, a declaration can also be a definition, such as:

class X
{
    // ...
};

So in a sense, since a definition is also a declaration, the sentence is not wrong.

The sentence would be wrong under the common-sense assumption that by declaration we mean a declaration which is not a definition, but when dealing with such terms it is probably better to keep in mind their formal definition.

Andy Prowl
  • 114,596
  • 21
  • 355
  • 432
  • But a class definition can also be a class declaration, right? E.g. [class.name]/2 "A declaration consisting solely of *class-key identifier;* is either a redeclaration of the name in the current scope or a forward declaration of the identifier as a class name." Therefore, you could put function bodies inside a class declaration -- if it contains the *class-specifier* / class definition.? – dyp Jun 02 '13 at 14:21
  • 3
    This is strictly incorrect. Your example is *forward* declaration. A declaration in general may be either a forward declaration or a definition. The quoted author isn't absolutely wrong, just unjustifiably imprecise. – Potatoswatter Jun 02 '13 at 14:27
  • @DyP: Yes, in strict terms, you are right. I was under the common-sense assumption that by "declaration" we mean "declaration which is not also a definition", but in fact that's not necessarily a correct assumption. I edited the answer, thank you – Andy Prowl Jun 02 '13 at 14:32
  • @Potatoswatter: You're right. As I wrote in the comment above, I was making a common-sense assumption which is not quite appropriate when dealing with formal terms. I edited the answer, thank you – Andy Prowl Jun 02 '13 at 14:32
0

From different point of view of OOP languages the interface mean a declaration of the class with her methods. In C++ you can put this in some header file. The implementation of the class mean implementation of the methods of this class. For example in Java you can use an abstract class where some methods are implemented and others are not. However the abstract clas or interface can not be instanced.

Mihai8
  • 2,871
  • 1
  • 18
  • 27
-1

a class declaration is where you declare the class hence. or as

From the C++ standard section 3.1:

A declaration introduces names into a translation unit or redeclares names introduced by previous declarations. A declaration specifies the interpretation and attributes of these names.

In essence the declaration tells the compiler a certain object, variable, etc with a certain name exists. There can be multiple declartions in a compile unit

so in a way

class X; // is declaration also known as forward declaration.

Is just a declaration. However there must always be just one and only one definition for each object / variable. And a definition is basically everything the compiler needs in order to create an instance of that class / object / struct / variable etc.

In c++ many times a class declaration can also be a definition or partial definition/

//declare a class and declare it's members.
class X { //declares X and starts to define it
   void test ();  //declare test method
   int b;         // declare b member
}

the full definition can then be in a .cpp file like this:

void X::test() {
  //test code
}

or in the class declration like this:

   class X {
      void test () {
         //test code
      }

Here is an extensive stackoverflow discussion on the differences:

What is the difference between a definition and a declaration?

I believe the author is recommending that you do not use the second type of declaration in which you declare and define the methods inside the class definition but in a separate c++ file. This is because of the C++ define only once rule, you do not want to include the same .h file from multiple files and have multiple definitions for the same class.

Here is another extensive discussion on why you should seperate:

Is is a good practice to put the definition of C++ classes into the header file?

Community
  • 1
  • 1
Dory Zidon
  • 9,328
  • 2
  • 20
  • 33
  • I didn't downvote, but the definition of the member functions isn't part of the definition of a class. The function body may appear inside the class definition, but that doesn't make it part of what is required to define a class. – dyp Jun 02 '13 at 15:19
  • I agree, declaring something means you have all the info to create it..so in essence you do not need the declaration of the methods to create the class instance..however, many times when putting all of them in the same place, and not splitting into header and cpp you get issues :) – Dory Zidon Jun 02 '13 at 15:21
  • No, `class X;` is a perfectly fine declaration of `X`. A declaration IMO is the introduction of a *name* with all necessary information to understand what that *name* refers to (Is it a variable, type, function? Template? etc.) A class *forward-declaration* is a pure declaration (or re-declaration), a class *definition* is both a (re-)declaration and a definition. A class has to be *defined* (and therefore, declared) before instances are created from it. – dyp Jun 02 '13 at 22:21
  • Isn't `void X::test() { //test code }` an implementation rather than a definition? – Post Self Jul 26 '17 at 17:10