What is the difference between using #include "stack. h" or using the class statement ?

Asked

Viewed 289 times

3

Having these two classes in two files .h distinguished.

a. h

class um {
  //(...)
};

two. h

#include "um.h" //opcao 1

class um; //opcao 2

class dois{
  public:
    void f1(um * p);
};

What is the difference between these two options and which one?

1 answer

5


They’re not options, since they don’t do the same thing.

In doing #include "um.h" you in fact includes the class header file (supposedly already defined), so that from then on (where it is including and recursively, if this location is also a header) you have access to all elements of its interface. That is, you can reference an attribute or method of that class - of course, depending on the declared scope and where the reference is being made.

In doing class um; you only declare the class (that is, empty because it has no definition), whose interface must be provided (defined) at some point. This is commonly called "forward declaration", and it is useful when you just need to inform that you will use this class, but do not need the details of its implementation yet. In other words, you say you will use, but do not want (or may not) to include the header for some reason.

An example of use is when a class A references a B that references A back. Class A includes the heading of B, but class B can only indicate that it "recognizes" class A and thus avoids a circular reference in includes:

B.h file:

class A;
class B
{
    A *a;
};

File a.h:

#include "b.h"
class A
{
    B *b;
};

But note that eventually the compiler needs to find the definition of a predefined class (as well as its implementation - the code of the methods), otherwise it will produce compilation errors and/or linkediting.

  • I was clarified. include of that class defined, to avoid any errors? However, in my problem using only the include for a type function void f1(nomeclass * p);, I get compile error, that the nomeclass is not identified, and when I put class nomeclass;, compiler already identifies, hence my question. And even more, having other classes, the same kind of function only works with includes. The order of includes has relevance?

  • (1) Yes, of course. (2) You need to see what the error is, because if everything is ok in the file . h this should not occur. (3) The order may have relevance depending on how you developed it. And not only that, but the build time and dependencies also flow from that structure. The ideal is to separate the files so that the classes are as independent as possible. And remember that vc don’t need have a . h for each class, you can also declare and define more than one class in the same file . h (if this makes sense in your problem domain).

  • It’s a big project, hence each class has its own .h. I think the problem lies in, something ambiguous, because what happens in my problem, is that I have two classes, and both classes need to include each other, that is, one class has a include . h, and in that include has, another include that calls itself. And I think that’s why the problem, hence having to use the class statement.

  • 1

    Well, in that case just make sure that no circular inclusion occurs. Read about the #pragma once and on the alternative with #ifndef. You can also use the forward declaration. In practice it is as you prefer, but I find it clearer to use one of the two previous alternatives if it is just to avoid circular reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.