There was a slight difficulty in splitting classes into several files. In .h I specify a class with prototypes and in the appropriate .cpp I initialize methods and objects. There is an app class that uses all other classes, but nevertheless, in these used classes, you need to refer to objects of the current instance of the app class.

I do as follows: in app.h I declared a class with prototypes, it has a declaration of class instances
material, control, objects.

Before that, he announced, no matter how wrong the expression, the class prototypes (so that at the next stage .h they would not include themselves through app.h).

class material; class control; class objects; 

Others .h have the same usage pattern, but I also need the app class in them, so it is done on app.h

In app.cpp, all the .h files are included, and in it when I initialize each class, I give them a link to the current app instance.

Is this the best way? Especially when it comes to complicating the structure of the application.

  • What is a> class with prototypes> class prototypes? This is not javascript. - atwice
  • I mean prototypes of methods that are in classes - Ni55aN
  • In C ++, this is called method declaration. - atwice
  • A declaration is a general concept where the function body is indicated. Prototype - without body function - Ni55aN
  • Not. The function with the body is the definition of the function. An ad is another. Nobody calls prototypes methods in C ++. - atwice

1 answer 1

1) Read about ifdef-guard or use #pragma once if this instruction is supported by your compiler. This allows you not to think about recursive dependencies when you include files.

2) .cpp files in large projects include in the following order:

  • first, precompiled headers . Perhaps you do not need yet;
  • the corresponding header file (i.e., for material.cpp, is material.h);
  • header files of all classes that are used in the implementation.

3) In files .h try to write as little as possible #include 's. It is advisable to manage ads, as in the question:

  class material; // ... 

#include has to be written for classes that your class inherits or allocates by value, and also for classes that are passed by value as an argument to a function. In all these cases, the compiler needs to know the size of the object. Forward-declaration will be small.