c++中重载运算符的相关问题

#include<iostream>
using namespace std;

class Myint
{
    friend ostream& operator<<(ostream& cout, Myint& myint);
public:
    Myint()
    {
        m_num = 0;
    }

    Myint& operator++()//前置递增
    {
        ++m_num;//先+
        return *this;//再返回
    }

    Myint operator++(int)//后置递增
    {
        Myint* temp;
        temp = new Myint(*this);//先记录当前数值
        m_num++;//再+
        return *temp;//再返回记录的数值
    }

private:
    int m_num;
};

ostream& operator<<(ostream& cout, Myint& myint)
{
    cout << myint.m_num;
    return cout;
}

void test01()
{
    Myint myint;
    cout << myint << endl;
    cout << ++myint << endl;
    cout << myint++ << endl;
    cout << myint << endl;
}

int main()
{
    test01();
    return 0;
}

之前我在定义temp时就是简单的定义一个临时变量,然后有人跟我说匿名临时变量不能引用,后来我把重载左移运算符的myint变量前的引用去掉,确实可以运行。
然后我就想是不是把temp变量存到堆区就可以不被删除了呢,是不是就可以取引用了呢,于是有上面的代码。结果报错原因和最开始一样,这到底是怎么回事啊

img

报错说temp是未定义的标识符

下面这段代码,函数需要返回一个指针地址 Myint*
如果return *temp; 这么返回,还是一个临时变量的拷贝
return temp; 这样才是返回地址,在堆区


Myint operator++(int)//后置递增
    {
        Myint* temp;
        temp = new Myint(*this);//先记录当前数值
        m_num++;//再+
        return *temp;//再返回记录的数值
    }