Should you rely on a different heading for the headings that it includes?

Assuming all headers are protected, let's say you had an abstract data type.

#include "that.h"
#include "there.h"

class Foo {
 protected:
  // Functions that do stuff with varOne and varTwo
 private:
  that varOne; 
  there varTwo;
 ...
};

Then in classes that inherit from foo (and thus include foo.h), would you also have to include this and this? Usually what I do includes everything the class needs, regardless of whether they will already receive them from another. Is this redundant?

+3
source share
6 answers

There is one drawback of redundancy, including header files that would otherwise be included directly: it forces the compiler to reopen and re-process them. For example, in

hijras:

#ifndef __A_H
#define __A_H
// whatever
#endif

bh:

#ifndef __B_H
#define __B_H
#include "a.h"
// whatever
#endif

c.cpp:

#include "a.h"
#include "b.h"
//whatever

ah . #ifndef preproc ah , - , , #ifndef #endif.

. - , , .

#pragma once , , a.h . .

+3

, Foo.h, , , .

cade, Foo.h :

class that;
class there;

class Foo
{
    public:

        void func( that &param ) = 0;
        void funcTwo( there &param ) = 0;
        ...
};
+1

, obj. . , , , , , . , .

+1

. , (.. ), #include. , , .

0

. , , , src header, .

.h , . stdio, , io, src, printf, .

, , , , (, , ), . .

, /exe, , -, , , .h - .

edit: inc1.h

#ifndef INC1_H
#define INC1_H
inc1_struct {                                                            
   int x;
};
#endif

inc2.h

#ifndef INC2_H
#define INC2_H
#include "inc1.h"
void inc2_f();
struct inc1_struct *inc2_callinc1();
#endif 

prog.cpp   #include "inc1.h"   #include "inc2.h"
  #   #include

void inc2_f() {
   printf("inc2_f\n");
}

struct inc1_struct *inc2_callinc1() {
   return (struct inc1_struct*) malloc(sizeof(struct inc1_struct));
}

int main(int argc, char **argv) {
   struct inc1_struct *s = inc2_callinc1();
   return 0;
}

, ,

struct inc1_struct *inc2_callinc1();

inc2.h

inc1.h inc2.h, inc2_callinc1() prog.cpp, inc1. .

0

, foo (, , foo.h), , ?

( ) , .

, , - --, , .

, , , - - , .

0

Source: https://habr.com/ru/post/1726413/


All Articles