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 8702007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:32:34+00:00 2026-06-13T02:32:34+00:00

I’m having a hard time getting this working in C++, I’ve managed it in

  • 0

I’m having a hard time getting this working in C++, I’ve managed it in C# but I’ve not used C++ much so I’m not sure on the syntax.

The purpose of this is for a simple state manager, each state inherits from a base class called “state”.

I’ve gotten overriding to work but I can’t seem to manage the polymorphism aspect. That is that I cannot have an object “State currentState” and set that object to equal “menuState” and have it run the desired function, I know this is because it is only finding the signature for the State class but I am unsure how to avoid it. Here’s some simplified code so that someone can help me understand.

// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// State.h
class State{
public:
  virtual void drawState();
};

// State.cpp
void State::drawState() {
    cout << "Base state.\n";
}

// MenuState.h
class MenuState: public State {
public:
  virtual void drawState();
};


// MenuState.cpp
void MenuState::drawState() {
    cout << "Menu state.\n";
    State::drawState();
}

int main ()
{
    State currentState;
    MenuState menuState;

    currentState = menuState;
    currentState.drawState();

    system("pause");
    return 0;
}

If you change “State currentState” to create an object of MenuState the code will work as I require it to, however I need it to be the parent class so that I can set the current state to other states I will create in the future such as GameState.

Thank you.

  • 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-13T02:32:36+00:00Added an answer on June 13, 2026 at 2:32 am

    Polymorphism doesn’t work with plain objects because of slicing. You have to use references or (smart) pointers. In your case, pointers, as references can’t be re-assigned:

    int main ()
    {
        State* currentState = NULL;
        MenuState menuState;
    
        currentState = &menuState;
        currentState->drawState(); //calls MenuState::drawState()
    
        NextState nextState; //fictional class
        currentState = &nextState;
        currentState->drawState(); //calls NextState::drawState()
    
        system("pause");
        return 0;
    }
    

    In your code:

    State currentState;
    MenuState menuState;
    
    currentState = menuState;
    

    the assignment slices menuState – it basically just copies the State part of it to currentState, losing all other type information.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.