C++作用域问题?

huangapple go评论62阅读模式
英文:

C++ Issue with scope?

问题

以下是您要的代码部分的中文翻译:

我正在尝试交换指针,使其指向在方法内部创建的新类实例的地址,但一旦返回到主函数,引用就丢失了,这是因为作用域的原因吗?有人可以解释一下吗?C/C++是否具有引用计数?

#include <iostream>

class MyClass {
public:
    int myNum;
    std::string myString;
    MyClass(int my_num, std::string my_string)
    {
        myNum = my_num;
        myString = my_string;
    }
};

void SwapRef(MyClass **p)
{    
    MyClass b(99, "test");
    *p = &b;
}

int main(int argc, char* argv[])
{
    MyClass a(1, "main");

    MyClass* aPtr = (MyClass*)0;
    aPtr = &a;

    std::cout << "myNum is: " << aPtr->myNum << " myString is: " << aPtr->myString << "\n";

    SwapRef(&aPtr);

    std::cout << "myNum is: " << aPtr->myNum << " myString is: " << aPtr->myString << "\n";

#ifdef _WIN32 || _WIN64
    system("pause");
#endif
}

输出:

myNum is: 1 myString is: main

myNum is: -858993460 myString is:

英文:

I am trying to swap a pointer to point to the address of a new class instance created inside a method, but once back to main, the reference is lost, is this because of scope? Could someone please care to explain? Does c/c++ have reference counting?

#include &lt;iostream&gt;

class MyClass {
public:
    int myNum;
    std::string myString;
    MyClass(int my_num, std::string my_string)
    {
        myNum = my_num;
        myString = my_string;
    }
};

void SwapRef(MyClass **p)
{    
    MyClass b(99, &quot;test&quot;);
    *p = &amp;b;
}

int main(int argc, char* argv[])
{
    MyClass a(1, &quot;main&quot;);

    MyClass* aPtr = (MyClass*)0;
    aPtr = &amp;a;

    std::cout &lt;&lt; &quot;myNum is: &quot; &lt;&lt; aPtr-&gt;myNum &lt;&lt; &quot; myString is: &quot; &lt;&lt; aPtr-&gt;myString &lt;&lt; &quot;\n&quot;;

    SwapRef(&amp;aPtr);

    std::cout &lt;&lt; &quot;myNum is: &quot; &lt;&lt; aPtr-&gt;myNum &lt;&lt; &quot; myString is: &quot; &lt;&lt; aPtr-&gt;myString &lt;&lt; &quot;\n&quot;;

#ifdef _WIN32 || _WIN64
    system(&quot;pause&quot;);
#endif
}

OUTPUT:

myNum is: 1 myString is: main

myNum is: -858993460 myString is:

答案1

得分: 1

以下是翻译好的部分:

这个函数

    void SwapRef(MyClass **p)
    {    
        MyClass b(99, "test");
        *p = &b;
    }

是错误的。具有自动存储期的对象 `b` 在函数退出后将不再存在。因此,使用表达式语句

    *p = &b;

在函数中分配给指针 `aPtr` 的本地对象的地址将在退出函数后变为无效。

对这样一个指针进行解引用将会导致未定义的行为。

相反,你可以写成

    **p = b;

利用编译器生成的默认复制赋值运算符。

如果在函数内部声明的对象具有静态存储期,那么该函数可能是正确的,比如:

    void SwapRef(MyClass **p)
    {    
        static MyClass b(99, "test");
        *p = &b;
    }

在这种情况下,它将在退出函数后仍然存在。

请注意,你应该包含头文件 `<string>`

    #include <string>

因为并不一定头文件 `<iostream>` 包含头文件 `<string>`。
英文:

This function

void SwapRef(MyClass **p)
{    
    MyClass b(99, &quot;test&quot;);
    *p = &amp;b;
}

is wrong. The object b having automatic storage duration will not be alive after exiting the function. So the pointer aPtr assigned in the function with the address of the local object using the expression statement

*p = &amp;b;

will be invalid after exiting the function.

Dereferencing such a pointer invokes undefined behavior.

Instead you could write

**p = b;

using the generated by the compiler the default copy assignment operator.

The function could be correct if the object declared within the function would have static storage duration like

void SwapRef(MyClass **p)
{    
    static MyClass b(99, &quot;test&quot;);
    *p = &amp;b;
}

In this case it will be alive after exiting the function.

Pay attention to that you should include header &lt;string&gt;

#include &lt;string&gt;

because it is not necessary that the header &lt;iostream&gt; includes the header &lt;string&gt;.

huangapple
  • 本文由 发表于 2023年2月8日 22:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定