Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在函数中创建的对象的范围_C++_Scope - Fatal编程技术网

C++ 在函数中创建的对象的范围

C++ 在函数中创建的对象的范围,c++,scope,C++,Scope,我在函数中创建了一个类的对象。创建的对象作为参数传递给另一个类。我希望当我退出创建对象的函数时,该对象必须被销毁 此外,对该对象的任何引用都必须无效,但我发现退出函数后引用的对象仍然有效。试图理解对象的范围。截取的代码如下所示 class TextWidget { public: TextWidget() { cout << "Constructor TextWidget" << endl;

我在函数中创建了一个类的对象。创建的对象作为参数传递给另一个类。我希望当我退出创建对象的函数时,该对象必须被销毁

此外,对该对象的任何引用都必须无效,但我发现退出函数后引用的对象仍然有效。试图理解对象的范围。截取的代码如下所示

class TextWidget
{
    public:
        TextWidget()
        {
            cout <<  "Constructor TextWidget" << endl;
        }

        TextWidget(int id)
        {
            _ID = id;
            cout <<  "Constructor TextWidget" << endl;
        }

        ~TextWidget()
        {
            cout <<  "Destructor TextWidget" << endl;
        }

        void printWidgetInstance()
        {
            cout << this << endl;
        }
        int _ID;
};

class AnotherWidget
{
    public:
        AnotherWidget()
        {
            cout << "Constructor AnotherWidget" << endl;
        }

        ~AnotherWidget()
        {
            cout << "Destructor AnotherWidget" << endl;
        }

        TextWidget* getTextWidget()
        {
            return _textWidget;
        }

        void setTextWidget(TextWidget* t)
        {
            _textWidget = t;
        }

        int getTextID() { return _textWidget->_ID; }

    private:
        TextWidget* _textWidget;
};


ButtonWidget b;
AnotherWidget a;

void fun()
{
    TextWidget t(7);
    b.setTextWidget(&t);
    a.setTextWidget(&t);
    a.getTextWidget()->printWidgetInstance();
    b.getTextWidget()->printWidgetInstance();
}

int main()
{
    fun();
    cout << "TextWidget in AnotherWidget is ";
    a.getTextWidget()->printWidgetInstance();
    cout << "Text ID in a is " << a.getTextID() << endl;
    getchar();
    return 0;
}

使用automatic storage duration(不使用new)声明的变量具有其作用域的生存期。在作用域之外访问变量会导致未定义的行为。

您需要了解什么是类、对象、指针和引用。类是内存中的代码,该代码处理成员变量。类不占用任何数据内存。当您创建一个对象时,您将实例化该类。此步骤将为类的一个对象实例的本地数据保留一些数据内存。要访问此实例,您将获得对对象(此对象)的引用。对象变量保存指向类实例的数据内存的指针

当一个对象被销毁时,被占用的内存块被列为空闲但未被清除。因此,如果您仍然有对该内存块的引用,您可以访问它。只要系统不把这个内存块用于其他目的,你仍然可以在那里找到你的位和字节

您可以声明一些变量:

long      long_array[10];
myclass   myobject;
这些变量顺序存储在一个内存块中。现在通过该阵列访问长_阵列后面的内存时:

长_数组[10]=12345;//有效范围为长数组[0]到长数组[9]


然后您将覆盖对象myobject的数据。

在上述情况下,没有未定义的行为。。每次我都会得到相同的结果。这是未定义的行为。您的编译器可以很好地处理此问题,但其他编译器无法处理。它未定义,因为保存对象的内存已被取消分配。因此,任何其他对象都可以自由覆盖数据。基本上,任何一个小的改变都会导致一个“无法解释的”错误突然出现在其他地方,因为其他地方决定将数据存储在对象“应该”的位置。qt和代码块。两者的结果相同。这两个都不是编译器,但是您可能在这两种情况下都使用了mingw。@sandy47rao“值似乎被保留”是未定义行为的一种可能影响。因此,你不能只根据程序输出来说“没有不确定的行为”。关于<代码>的ID C++有很多规则来处理许多有趣的案例,而在你来得太晚之前,没有人告诉过你。在这种情况下,和在标准库实现中暴露于与相同标识符的冲突中。也许可以侥幸逃脱,但为什么要冒险呢?
long      long_array[10];
myclass   myobject;