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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:25:47+00:00 2026-06-11T13:25:47+00:00

I’m new to c++ programming and i’m getting a weird value coming back from

  • 0

I’m new to c++ programming and i’m getting a weird value coming back from a char* variable that i’ve set, depending on how i use it. I’m obviously doing something really stupid but I can’t see the problem. The next couple of paragraphs describe the setup(badly), but it’s probably easier to just look at the output and code.

Basically, I have a couple of classes – Menu and MenuItem. The MenuItem class has a name which is of type char*. Depending on how i’m using the menu items, i’m getting strange results when i do a getName() on the MenuItems.

I have a Machine class which has a state (TestState). This TestState creates a Menu containing MenuItems. When i create a TestState in my main function and have it print out the menu I get what I expect. When i create a Machine which contains a TestState and ask it to print the menu it prints something weird for the name of the root item in the menu.

Output – Last line I’m expecting menuItem1, but i get Hâ∆Hã=ò

Output direct from TestState Object

Displaying menu state 
menuItem1
root not null 
menuItem1


Output from TestState within Machine

Displaying menu state 
menuItem1
root not null 
Hâ∆Hã=ò

Here’s my code –
Main.cpp

#include "Menu.h"
#include "Machine.h"
#include <iostream>

using namespace std;

Machine m;
TestState t;

int main(void) {
    cout << "Output direct from TestState Object" << endl << endl;
    t = TestState();
    t.print();


    cout << endl << endl << "Output from TestState within Machine" << endl << endl;
    m = Machine();
    m.printCurrentState();
}

Menu.h

#ifndef Menu_h
#define Menu_h

#include <stdlib.h>

class MenuItem {
public:
    MenuItem();
    MenuItem(const char* itemName);
    const char* getName() const ;

protected:
    MenuItem *next;
    const char* name;
};

class Menu {
public:
    Menu(MenuItem *rootItem);
    Menu();
    void setRoot(MenuItem *r);
    MenuItem* getRoot() ;
protected:
    MenuItem *root;
};

#endif

Machine.h

#ifndef MACHINE_H_
#define MACHINE_H_

#include "Menu.h"

class TestState;
class Machine;

class TestState {
public:
    TestState();
    virtual ~TestState();
    void print();
protected:
    Machine* machine;
    MenuItem menuItem1;
    Menu menuMain;
};

class Machine {
public:
    Machine();
    void printCurrentState();
protected:
    TestState testState;
};

#endif /* MACHINE_H_ */

Machine.cpp

#include "Machine.h"
#include <iostream>
using namespace std;

TestState::TestState() {
    menuItem1 = MenuItem("menuItem1");
    menuMain = Menu(&menuItem1);
}

void TestState::print(){
    cout << "Displaying menu state " << endl;
    cout << menuItem1.getName() << endl;

    if (menuMain.getRoot() == NULL) {
        cout << "root is null" << endl;
    } else {
        cout << "root not null " << endl;
        cout << menuMain.getRoot()->getName() << endl;
    }
}

TestState::~TestState() {
    // TODO Auto-generated destructor stub
}

Machine::Machine() {
    testState = TestState();
}

void Machine::printCurrentState() {
    testState.print();
}

Any help would be appreciated. I’m a bit lost.
Thanks
Dave

  • 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-11T13:25:48+00:00Added an answer on June 11, 2026 at 1:25 pm

    I suspect what’s happening is Menu.root is pointing to a temporary object somewhere. You’ll notice that you make a copy of your machine in your main function:

    // in main():
    m = Machine(); // makes a machine, then copies it
    

    That machine has a TestState, which has a MainMenu, which has a pointer to a MenuItem:

    // in MenuItem class definition:
    MenuItem *root;
    

    That pointer gets initialized to the address of a member of your original Machine. Problem is, that object only exists for a short time: it’s destroyed when the copy is complete, leaving you with a dangling pointer.

    In other words, you need to make sure that when you copy an object that contains pointers, you update those pointers to reflect the address of the duplicated object instead of the old one.

    You need to add copy constructors like the following:

    Machine::Machine(const Machine& other)
    {
        teststate = other.teststate;
        teststate.machine = this; // you will need to expose TestState.machine to Machine
    }
    
    TestState::TestState(const TestState& other)
    {
        machine = other.machine; // Machine copy constructor modifies this for us
    
        menuItem1 = other.menuItem1; // these 3 we have to do
        menuItem2 = other.menuItem2;
        menuMain = other.menuMain;
    
        menuMain.setRoot(&menuItem1); // update pointers to be to persistent copies
        menuItem1.setNext(&menuItem2);
        menuItem2.setNext(NULL);
    }
    

    You might notice that your system is rather brittle. I’d recommend relying on pointers between objects less, because dragons be down that road.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.