2

I'm analying an Operating Systems project for school and came across this header file:

//kernelev.h

#ifndef _KERNELEV_H 
#define _EVENT_H_

typedef unsigned char IVTNo;

class Thread;
class PCB;
class KernelSem;

class KernelEv {
public:
     KernelEv (IVTNo ivtNo);
     ~KernelEv();
     int wait(int MaxTimeToWait);
     void signal();

[...]

Now, when writing the complete definitions of these methods (KernelEv, ~KernelEv, wait and signal), they used the attributes of the classes Thread, PCB and KernelSem. What would be the difference between generally introducing for instance #include Thread.h; #include KernelSem.h; and just declaring the classes like this: class Thread; Are there differences in data access rights? Or it's somehow completely different?

Thanks for your help, I hope my question is clear enough.

R. Martinho Fernandes
  • 209,766
  • 68
  • 412
  • 492
  • Doesn't sound like quite the same question to me – Dave Aug 26 '13 at 10:44
  • @Dave doesn't sound the same, but the answers are enough to answer this question. – juanchopanza Aug 26 '13 at 10:49
  • 3
    Take care with your include guards! You're using [reserved names](http://stackoverflow.com/questions/228783) and, more critically, the two names don't match. – Mike Seymour Aug 26 '13 at 10:54
  • Actually, the forward declaration topic works for me. Thank you, juanchopanza! – Stefan Wœlfisch Aug 26 '13 at 10:55
  • The main point of using forward declarations (manual breaking circular dependencies, because C++ parser is essentially a single-pass one) is mentioned only in a minor comment. – Vlad Aug 26 '13 at 11:25

3 Answers3

3

First, note that if you only introduce the classes, you won't be able to use the methods;

class Thread;

Thread x; // compile error: size of x unknown
Thread* x; // this is ok
// set x to some valid thread, maybe as a parameter
x->signal(); // compile error

But it makes no difference whether your declarations are in a header or included in your file. That is, you could replace the include line with a copy of the header and everything would work perfectly fine (every line in the above example would be valid). There are many reasons not to, however. Ease of maintenance would be the top issue, along with readability and modularity. It would also be less applicable to compiler caching (so would generally take longer to compile)

Dave
  • 36,791
  • 8
  • 53
  • 96
2

If you only have a declaration class A; and not the full class definition, then the type is said to be incomplete. This can be used in limited ways; you can do things that only need knowledge that the class exists, for example:

  • Declare a pointer or reference to it;
  • Declare a function using it as a parameter or return type;
  • Declare (but not define) an external variable of that type.

You can't do anything that requires knowledge of the class members, size, or other details given by the definition, for example:

  • Define a variable of that type;
  • Define a function using it;
  • Access any members or nested declarations;
  • Inherit from it;
  • Apply sizeof to it.
Mike Seymour
  • 235,407
  • 25
  • 414
  • 617
0

If I am not wrong, you are asking about Declaration vs Definition. They are different but related.

I forward you to this two posts googled as "c++ declaration definition", since they will explain much better than me :)

What is the difference between a definition and a declaration?

http://www.cprogramming.com/declare_vs_define.html

Just comment that your class A; is a declaration, while the includes Thread.h surely will have plenty definitions inside (and maybe declarations too).

--------------------- edit:

About forward declarations as commented by @Dave bellow:

class B ; // needed declaration

class A {
  B field ;
} ;

class B {
  A field ;
};
Community
  • 1
  • 1
Alfonso Nishikawa
  • 1,856
  • 1
  • 17
  • 32
  • Oh, no. I (think I) know the difference between those two. I just wanted to know what is my incentive to use the "incomplete class declaration" class Thread; class PCB; etc. instead of including the corresponding header files (which do exist): #include "Thread.h" #include "PCB.h" ... etc. What are the differences and why should I ever do that? Any ideas? – Stefan Wœlfisch Aug 26 '13 at 10:52
  • 1
    @StefanWœlfisch it's often used if you need two classes to be aware of each other, or if you have modules of a program which only ever handle pointers to certain classes (in which case giving them the full class definition only serves to lengthen the compile time) – Dave Aug 26 '13 at 10:54
  • The compiler will know nothing about internals of Thread if you dont define it. So, why is that declaration there? :S (where is the self-reference or loop?) – Alfonso Nishikawa Aug 26 '13 at 10:54
  • Well, if the header doesn't use those methods, it doesn't need to know them. The implementation (which is not available in the include directory) will know the full definition. By just using declarations, the header can compile faster. Or if it later defines them, it's doing the first point I mentioned (classes are aware of each other) – Dave Aug 26 '13 at 10:59
  • @Dave: nothing to object :) – Alfonso Nishikawa Aug 26 '13 at 11:00