Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:35:44+00:00 2026-06-10T13:35:44+00:00

class Game { private: string title; bool running; State currentState; public: sf::RenderWindow window; void

  • 0
class Game {
  private:
    string title;
    bool running;
    State currentState;

  public:
    sf::RenderWindow window;
    void setup();
    void run();
};

I have a variable called currentState. This is State:

#ifndef STATE_HPP
#define STATE_HPP

using namespace std;

class State {
  private:

  public:
    void start();
    void update();
    void render();
};

#endif

And then I have a class called PlayState, which inherits State:

#ifndef PLAY_STATE_HPP
#define PLAY_STATE_HPP

#include <SFML/Graphics.hpp>

#include "Game.hpp"
#include "State.hpp"

using namespace std;

class PlayState : public State {
  private:
    sf::CircleShape shape;
    Game game;

  public:
    PlayState();
    void start();
    void update();
    void render();
};

#endif

On my Game.cpp, I am creating currentState, by doing:

currentState = PlayState();

The problem is, though, that it’s not working. currentState.update() is state.update(). It seems that I am not overriding the State methods when I create PlayState.

Here’s PlayState.cpp:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <stdio.h>

#include "PlayState.hpp"

PlayState::PlayState() {
  printf("heyyy\n");
}

void PlayState::start() {
  shape.setRadius(100.f);
  shape.setOrigin(20.0f, 20.0f);
  shape.setFillColor(sf::Color::Green);
}

void PlayState::update() {
  sf::Event event;
  while (game.window.pollEvent(event)) {
    if (event.type == sf::Event::Closed) {
      game.window.close();
      //running = false;
    }
  }

  printf("here\n");
}

void PlayState::render() {
  printf("here\n");
  game.window.clear();
  game.window.draw(shape);
  game.window.display();
}

Any ideas about how I can ‘override’ those methods? Thank you.

EDIT
I had to make State.cpp functions virtual so that they can be overriden.
I also had to define State *currentState as a pointer and create PlayState with “currentState = new PlayState();”.
Also, now I access .update and .draw with ->update() and ->draw().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:35:45+00:00Added an answer on June 10, 2026 at 1:35 pm

    Two problems. As @McAden said, the functions in State that are to be overridden in PlayState need to be marked virtual. The other problem is that the data member currentState in Game has type State. When you assign an object of type PlayState to it, it gets the State part of the PlayState object, but not the derived parts. This is called “slicing”. To prevent it, make currentState a pointer to State, and when you create that PlayState object, assign its address to the Game object’s currentState.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
import java.util.Scanner; public class Game { private String startYorN; private int generateTestValues; private int
public void openOptions() { Intent intent=new Intent(this,Game.class); //intent.putExtra(razmer, level); startActivity(intent); // Intent intent=new Intent(this,Options.class);
import java.util.*; import java.io.*; public class LootGenerator{ public static void main (String[] args) throws
I have a JFrame here: import javax.swing.JFrame; public class Game extends JFrame { private
Code: import java.awt.Dimension; import javax.swing.*; public class Game extends JFrame { private static final
public class Game { public Game( boolean createstage, //For sorting purposes int slength, int
Let's assume public abstract class Game { // Base } public class Poker :
I have a class with the following declaration of the fields: public class Game
I had a class hierarchy setup like so: public abstract class GameController public abstract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.