Domanda

I have 3 classes that derive from one another - GameScreen is the base class to which MenuScreen is derived from. I then have a third class 'TitleScreen' which derives from 'MenuScreen'.

The flow is basically from the base class: 'GameScreen' -> 'MenuScreen' -> 'TitleScreen'

The base class 'GameScreen' has no parameters in it's constructor, like wise with 'TitleScreen', however I need a parameter for 'MenuScreen'. I currently have the header files as:

GameScreen.h

class GameScreen
{
public:
    GameScreen();
}

MenuScreen.h

class MenuScreen : public GameScreen
{
public:
    MenuScreen(std::string title);
}

TitleScreen.h

class TitleScreen : public MenuScreen
{
public:
    TitleScreen(std::string title) : MenuScreen(title);
}

What I'm having difficulty trying to understand is if this is possible in C++ (I'm following a C# sample for Game State Management which does this). Reading through class inheritance in some books I have it only covers parameters inherited from the base class, were as my sample base class has no parameters.

È stato utile?

Soluzione

  1. You are missing ; after each class declaration.

  2. If you write TitleScreen(std::string title) : MenuScreen(title) you are defining the body of the method but the body is missing... so you should put just declaration to your TitleScreen.h :

    class TitleScreen : public MenuScreen
    {
    public:
        TitleScreen(std::string title);
    };
    

    and then place the body of the constructor to TitleScreen.cpp:

    #include "TitleScreen.h"
    
    TitleScreen::TitleScreen(std::string title) : MenuScreen(title)
    {
        // ..
    }
    

Edit: fixed the terminology accordint to this question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top