Non-existent C++ class pointers, how does it work?

Asked

Viewed 61 times

1

Sometimes I find myself in 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;
}

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

In this case it still doesn’t make sense yet, where it creates a "Prototype class"

#ifndef CPU_HPP
#define CPU_HPP

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

class GameBoy;

Creates 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 class members.

What’s the name of it? I’m looking at some C++ books that I have and I don’t see anything like it, and what’s the point?

  • 1

    Of course it makes sense, is for the class to be accessible when Ui create the object from Form (which is an Xml that will generate .cpp only in pre-build), this cpp for the form is generated dynamically. And pre-create classes is useful so you need to create a var that type is class Gameboy this in HPP, but the class is only included in CPP, so you already pre-declare and put your var there class FooBar {private: Gameboy *var1; }.

  • What is the name of this technique and what are its advantages?

  • 1

    An advantage is having the accessible pre-class to be able to declare variables of the type of this specific class before including it, but perhaps there should be "more advantages", however if you are going to make a #include direct on another . hpp would not have much sense to pre-declare.

1 answer

2


In the form used has no meaning. This is called forward declaration, this is necessary in compilers that does the analysis of the code in just one step (interestingly this does not occur in C++, but by compatibility with C, because C++ has too much context to be analyzed and other more specific reasons, it is not done).

It is useful when you need it in cases of cyclical use, ie, you will use one type in another type that will be used in the first type. How do you declare something that doesn’t exist? Then you make a simple statement without saying what you will have inside for the type to exist and can be used in another type, then this type existing you can use it to declare completely the previous type that depends on this statement.

The pointer itself has nothing to do with it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.