C ++参考如何工作 [英] How C++ reference works

查看:48
本文介绍了C ++参考如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中工作了15年后,我发现我不完全理解引用...

After working 15 years in C++ I found that I don't understand references completely...


class TestClass
{
public:
    TestClass() : m_nData(0)
    {
    }

    TestClass(int n) : m_nData(n)
    {
    }

    ~TestClass()
    {
        cout << "destructor" << endl;
    }

    void Dump()
    {
        cout << "data = " << m_nData << "  ptr = 0x" << hex << this << dec << endl;
    }

private:
    int m_nData;
};

int main()
{
    cout << "main started" << endl;

    TestClass& c = TestClass();
    c.Dump();

    c = TestClass(10);
    c.Dump();

    cout << "main ended" << endl;

    return 0;
}

// prints:
// main started
// data = 0  ptr = 0x0012FF54
// destructor
// data = 10  ptr = 0x0012FF54
// main ended
// destructor

我从此测试中了解到TestClass实例是在堆栈上创建的(对吗?),并由第一个TestClass构造函数初始化。分配此实例时:何时加载主函数或执行引用分配?

I understand from this test, that TestClass instance is created on the stack (is this correct?) and initialized by first TestClass constructor. When this instance is allocated: when main function is loaded, or reference assignment is executed? When it is destroyed?

第二个引用分配对象地址不变后。这是否意味着析构函数和构造函数将应用于同一内存区域?还是内存被释放(动态地在堆栈上?)并再次分配?

After second reference assignment object address is not changed. Does this mean that destructor and constructor are applied to the same memory area? Or memory is deallocated (dynamically? on the stack?) and allocated again?

我了解有关堆栈和堆分配对象生命周期,其构造函数和析构函数的所有知识。但是我不明白该程序到底发生了什么。

I know everything about stack and heap-allocated objects lifetime, their constructors and destructors. But I cannot understand what exactly happens in this program.

编辑:
谢谢大家。我试图在此测试中重现其他(更复杂)的程序行为。您的评论帮助我理解了我的错误以及我正在与之抗衡的另一个程序...

Thanks to all. I tried to reproduce in this test some other (more complicated) program behavior. Your comments helped me to understand both my mistake and another program I am fighting with...

固定代码为:


int main()
{
    cout << "main started" << endl;
    TestClass t;

    TestClass& c(t);
    c.Dump();

    c = TestClass(10);
    c.Dump();

    cout << "main ended" << endl;
    return 0;
}


推荐答案

您的代码遭受多个问题的困扰,最终将变得毫无意义。但是,让我们来解决它。

Your code suffers from multiple problems and ultimately won't make sense. However, let's hack through it.

1)您只能将临时绑定到 const 引用,从而延长了其寿命:

1) You can only bind a temporary to a const reference, thus extending its lifetime:

const TestClass & c = TestClass();

2)现在我们不能使用 dump ,因为您没有声明它 const

2) Now we can't use dump, because you didn't declare it const:

void Dump() const

3)说 c = TestClass()是一个分配。但是, c 现在是对常量的引用,由于赋值是非常量的(由于显而易见的原因),因此无法将其分配给常量。让我们来解决这个问题:

3) Saying c = TestClass() is an assignment. However, c is now a reference-to-const, which cannot be assigned to, since assignment is non-constant (for obvious reasons). Let's hack around this:

const_cast<TestClass&>(c) = TestClass(10);

现在,我们为临时但扩展了的对象 c ,一切都应如此:

Now we've assigned a new value to the temporary-but-extended object c, and all is as it should be:

main started
data = 0  ptr = 0x0xbfa8219c
destructor
data = 10  ptr = 0x0xbfa8219c
main ended
destructor

指针是相同的,因为只有一个对象,即 c 引用的(临时)对象。分配给它的是一种通常是未定义行为的hack,但是出于演示的目的,我们将其忽略。

The pointers are the same because there's only one object, namely the (temporary) one referenced by c. Assigning to it is a hack that's undefined behaviour in general, but we get away with it for the purpose of this demonstration.

中间破坏者是第二个临时 TestClass(10)

The intermediate destructor is that of the second temporary TestClass(10).

这篇关于C ++参考如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆