So I have a class A, where I want to call some class B functions. So I include “b.h”. But, in class B, I want to call a class A function. If I include “a.h”, it ends up in an infinite loop, right? What can I do about it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Each class (A and B) should have a header file and an implementation file.
Each header file (e.g.
A.h) should not include the other header file (e.g.B.h) but may include a forward reference to the other class (e.g. a statement likeclass B;), and may then use pointers and/or references to the other class in its declaration (e.g.class Amay contain aB*as a data member and/or as a method parameter).Each CPP file (e.g.
A.cpp) may include more than one header file (e.g.A.handB.h). It’s recommended that each CPP file should include its own header file first (e.g.A.cppshould includeA.hand thenB.h, whereasB.cppshould includeB.hand thenA.h).Each header file should contain only the declaration, and not the definition of the class: for example it will list the signatures of the class’ methods, but not the method bodies/implementations (the method bodies/implementations will be in the
.cppfile, not in the header file). Because the header files don’t contain implemention details, they therefore don’t depend on (don’t need to see) details of other classes; at most they need to know that, for example,Bis the name of a class: which it can get from a forward declaratin, instead of by including a header file in another header file.