The difference between #include .h and just stating class A; in C++ -
this question has answer here:
- when can use forward declaration? 12 answers
i'm analying operating systems project school , came across 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 complete definitions of these methods (kernelev, ~kernelev, wait , signal), used attributes of classes thread, pcb , kernelsem. difference between introducing instance #include thread.h; #include kernelsem.h; , declaring classes this: class thread; there differences in data access rights? or it's somehow different?
thanks help, hope question clear enough.
first, note if introduce classes, won't able use methods;
class thread; thread x; // compile error: size of x unknown thread* x; // ok // set x valid thread, maybe parameter x->signal(); // compile error
but makes no difference whether declarations in header or included in file. is, replace include
line copy of header , work fine (every line in above example valid). there many reasons not to, however. ease of maintenance top issue, along readability , modularity. less applicable compiler caching (so take longer compile)
Comments
Post a Comment