L
L
Legebocker2017-08-17 16:18:21
Programming
Legebocker, 2017-08-17 16:18:21

Header in header and is it possible to make a game on headers alone?

There is a lot of Header(1) in this question, so I'll put a counter on it :)
I love order too much. And to keep the code clean, I use Headers(2).
Hmm... it sounds like an advertisement, but still. I decided to do this. Write a bunch of Headers (3). Each will have its own. And then add them to the main file and write out all the functions. You can do this or there is something better for order? Is it possible to do Header(4)
in Header'e(5)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2017-08-17
@saboteur_kiev

The game can be made on the same libraries. Write a bunch of libraries, each with its own.
And then call all the functions from the connected libraries.
The game can be made in one main file. Do not include anything in it, write in spaghetti code.
In general, you can write programs with three characters, just

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.-----------------------.

D
devalone, 2017-08-17
@devalone

What did I just read? You don't seem to understand why headers are needed, there should be definitions of everything that you use, and the implementation is in *.cpp. A common practice is to use .h with the definition and a .cpp file with the implementation for classes, the file name is the same as the class name, in .h you need to use #pragma once or the include guard crutch. For example:
Color.h:

#pragma once

// или так
// #ifndef COLOR_H
// #define COLOR_H

class Color {
public:
  Color(unsigned char red, unsigned char green, unsigned char blue);
private:
  unsigned char red, green, blue
};

// #endif

color.cpp:
#include "Color.h"

Color::Color(unsigned char red, unsigned char green, unsigned char blue)
  : red(red), green(green), blue(blue)
{
}

There is one exception to the rule - templates, they are usually shoved into the header with the .hpp extension, so that it can be seen that these are templates, cpp for templates cannot be done.
Of course, you can write all the code in headers, but then at the slightest change, the entire project is recompiled.

M
Mercury13, 2017-08-17
@Mercury13

Such a mechanism is called "one compilation unit" and it has the right to life: on one processor, the full assembly time will be the smallest, because some libraries (SqLite, Google Test) are distributed this way. True,
the game is usually a large-scale project, and the more code there is, the longer it takes to compile to check the changes.
Therefore, the code is usually scattered into compilation units (*.cpp) in accordance with its internal logic, and a header (*.h) is assigned to each unit (except for the entry point), which shows what this unit does. I repeat, it only describes what it does - all the code in the CPP. And in most cases, the longest build stage is linking (especially with link optimization aka LTO).
The only thing that in 80% of cases cannot be thrown into CPP is templates.
To prevent one header from being connected several times, there is an include guard. True, if you have not thought through the dependency between the headers, a cyclic inclusion may come across - this is harmful, but only because the program may not compile.
In order to avoid cyclic inclusions, two headers are most likely required for saving: one is responsible for the actual saving procedure and is connected to a single module - the menu system. The second one is for some general functions that allow a tiled background, a projectile, a monster to be saved into an abstract stream ... It is called something like SaveUtils.h and is connected everywhere.
(Note: Both SqLite and Google Test were developed according to the traditional scheme, with a project from a bunch of CPPs. SqLite is assembled into one big *.c with automation, and I even downloaded the traditional code - on a 5-meter file, some versions of Embarcadero crashed with a lack of memory. In Google Test there is an all.cpp file or something similar, in which #include other CPPs - the user connects all.cpp to the project, and the library takes a minimum of his time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question