include - Mutual Import C++ -


i know reoccurring question, far have seen answer “if have mutual imports doing wrong”.

that why have more of general understanding question 1 tied specific code.

okay, example. have data structure dynamic automaton. states of automaton structs attribute “transitions”. “transitions” dynamic linked list. list elements structs have attribute state object...

so:

// state.h #include "transitionlist.h"  state{     transitionlist transitions; // transitions going out state }  // tranistion.h #include "state.h"  transition{     transition* next_transition; // next transition in transitionlist     state* successor; // pointer next state in automaton }   // transitionlist.h #include "tranisiton.h"  transitionlist{     // *code* class linked list of transitions } 

so have state.h -> tranistion.h -> transitionlist.h -> state.h -> …

this circular... conceptual mistake? don't see how bad layout formal point of view.

please enlighten me :)

conceptually it's fine. it's physically not going work because c++ doesn't have physical module system. #include directive inserts copy of included file, circular includes produce source stream of infinite length.

you need present compiler classes in order can compile, combination being known "translation unit".

where have class holds pointer class has not yet been declared, have "forward declare" it:

class state; 

that trick allows 1 of class headers not need include full definition of holds pointer to, , breaks loop.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -