Pointers of non-existent classes in C ++, how does it work?

1

Sometimes I run into situations like this:

#ifndef CRIARVENDA_H
#define CRIARVENDA_H

#include <QDialog>
#include "cliente.h"

namespace Ui {
class CriarVenda;
}

class CriarVenda : public QDialog
{
    Q_OBJECT

public:
    explicit CriarVenda(QWidget *parent = 0);
    ~CriarVenda();

private:
    Ui::CriarVenda *ui;
    Cliente *cliente;
};

#endif // CRIARVENDA_H

in this case

namespace Ui {
class CriarVenda;
}

It is created a kind of a prototype of a class that will still be created which does not make much sense.

In this case it still does not make sense yet, where it creates a "Prototype class"

#ifndef CPU_HPP
#define CPU_HPP

#include <array>
#include <cstdint>
#include <functional>

class GameBoy;

Create a pointer to it

GameBoy* gameboy;

    public:
        CPU( GameBoy& core );

#include "GameBoy.hpp"
#include "CPU.hpp"
#include "RAM.hpp"

CPU::CPU( GameBoy& core ) :
    gameboy( &core ) {
    initOpcodeArray();
}

std::uint8_t CPU::readMem( std::uint16_t address ) {
    gameboy->ram.readMem( address );
}

void CPU::writeMem( std::uint16_t address, std::uint8_t value ) {
    gameboy->ram.writeMem( address, value );
}

Then call up the members of the class.

What is the name of this? I'm looking at some C ++ books I have and I do not see anything like that, and what's the use?

    
asked by anonymous 18.12.2017 / 18:39

1 answer

2

In the form used it does not make sense. This is called forward declaration , this is needed in compilers that performs code analysis on only one step (curiously this does not occur in C ++, but because of C compatibility, because C ++ has too much context to be parsed and other more specific reasons, it is not done).

It is useful when you need it in cyclic use cases, ie you will use a type in another type that will be used in the first type. How to declare something that does not exist? So you make a simple statement without saying what it will have inside for the type to exist and can be used on another type, then this type existing you can can use it to completely declare the previous type that depends on this statement.

The pointer itself has nothing to do with it.

    
19.12.2017 / 14:43