0

我是 C++ 编程的新手,我从我设置的 char* 变量返回一个奇怪的值,这取决于我如何使用它。我显然在做一些非常愚蠢的事情,但我看不到问题所在。接下来的几段描述了设置(很糟糕),但只看输出和代码可能更容易。

基本上,我有几个类 - Menu 和 MenuItem。MenuItem 类有一个 char* 类型的名称。根据我使用菜单项的方式,当我在 MenuItems 上执行 getName() 时会得到奇怪的结果。

我有一个具有状态(TestState)的机器类。此 TestState 创建一个包含 MenuItems 的菜单。当我在我的 main 函数中创建一个 TestState 并让它打印出菜单时,我得到了我所期望的。当我创建一个包含 TestState 的机器并要求它打印菜单时,它会为菜单中的根项目的名称打印一些奇怪的东西。

输出- 最后一行我期待menuItem1,但我得到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ã=ò

这是我的代码 - 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();
}

菜单.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

机器.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_ */

机器.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();
}

任何帮助,将不胜感激。我有点失落。谢谢戴夫

4

4 回答 4

4

我怀疑正在发生的事情是Menu.root指向某个地方的临时对象。您会注意到您在 main 函数中复制了您的机器:

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

那台机器有一个TestState,它有一个MainMenu,它有一个指向 a 的指针MenuItem

// in MenuItem class definition:
MenuItem *root;

该指针被初始化为原始机器成员的地址。问题是,该对象只存在很短的时间:当复制完成时它被销毁,留下一个悬空指针。

换句话说,您需要确保在复制包含指针的对象时,更新这些指针以反映复制对象的地址而不是旧对象的地址。

您需要添加如下复制构造函数:

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);
}

您可能会注意到您的系统相当脆弱。我建议少依赖对象之间的指针,因为龙会走这条路。

于 2012-09-07T22:41:15.810 回答
1
TestState::TestState() {
    menuItem1 = MenuItem("menuItem1");
    menuItem2 = MenuItem("menuItem2");
    menuMain = Menu(&menuItem1);
    menuMain.add(&menuItem2);
}

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

构造Machine函数构造一个临时变量TestState并将其数据成员复制到Machine::testState. Machine构造函数完成后,临时对象消失TestState,但Machine::testState.menuMain.root仍指向临时对象的成员。

怎么修:

了解初始化变量的各种不同方式的含义,以及如何在构造函数中使用初始化列表。

于 2012-09-07T22:44:14.550 回答
1

而不是写Thing name = Thing(ctorParams);make name 一个指针并使用new Thing(ctorParams);. 看起来你以为你在使用指针,但它在没有 new 关键字的情况下工作,所以你继续并没有使用它们,这会导致你的错误。

于 2012-09-07T22:57:46.853 回答
0

Machine没有复制构造函数,因此Machine(特别是 within TestState)内的各种指针和引用都指向垃圾。

于 2012-09-07T22:17:38.307 回答