-2

I am trying to understand project structure in c++, I am finding it difficult to get my head around class structure and header files.

Extract from article 1 (linked at bottom of this post)

By convention, include directory is for header files, but modern practice > suggests that include directory must strictly contain headers that need to be exposed publicly.

My first question of this process is with regards to a separate class file that is within the include directory.

What is purpose of exposing your headers?

Following on from this, looking at an example of an exposed header file. Linked in the following GH repo: https://github.com/AakashMallik/sample_cmake

How does the Game_Interface class relate back to the Game_Engine?

game_interface.h

#pragma once

#include <game_engine.h>

class GameInterface
{
  private:
    GameEngine *game;

  public:
    GameInterface(int length);
    void play(int num);
};

I have looked else where for a simple explanation of this process but, all I have found so far is nothing that can be understood in the context of this example.

Fairly new to C++ background in web technologies.

Link to article 1: https://medium.com/heuristics/c-application-development-part-1-project-structure-454b00f9eddc

Howard
  • 9
  • 3
  • 1
    I would not include `game_engine.h` in `game_interface.h` if a forward declaration would be enough. – drescherjm May 06 '19 at 21:34
  • ***What is purpose of exposing your headers?*** To allow a client application to create and use the objects. – drescherjm May 06 '19 at 21:39
  • Related: https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files – drescherjm May 06 '19 at 21:42
  • ***How does the Game_Interface class relate back to the Game_Engine?*** I would guess that `Game_Interface` will allocate a single instance of `GameEngine` in its constructor and then use that `game` object in its `play()` member function. – drescherjm May 06 '19 at 21:43

1 Answers1

0

What is purpose of exposing your headers?

Sometimes you may be developing some functionality or a library. You might want to help some other person or customer or client by sharing the functionality of your code. But you don't want to share the exact working details.

So for instance you wish to share an Image processing functionality which applies beautiful filters to it. But at the same time you don't want them to exactly know how did you implement it. for such scenarios, you can create a header file, say img_filter.h having function declaration -

bool ApplyFilter(const string & image_path);  

Now you can implement entire details in img_filter.cpp:

bool ApplyFilter(const string & image_path)
{
....
    // Implementation detail
...
}

Next you can prepare a dll of this file which could be used by your client. For reference of working, parameters, usage etc. you can share the img_filter.h.

Relation with Interface:
A well defined interface is generally nice to have so you can change implementation details transparently, which means, that HOW you implement the details don't matter as long as the interface or the function name and parameters are kept intact.

Saket Sharad
  • 392
  • 2
  • 11
  • @Howard (1) For creating and using dll. `https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019`. (2) Interfaces in C++- `https://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm` – Saket Sharad May 07 '19 at 09:34