English 中文(简体)
C++ 从功能中接收物体
原标题:C++ ampersand receiving object from function

我有以下简单的法典,有一类,包括普通建筑商和复印件建筑商。

class largeObj
{
public:
    largeObj()
    {
        printf("
正常建筑
");
    }

    largeObj(const largeObj& mv)
    {
        printf("
制模
");
    }

    ~largeObj()
    {
        printf("
拆除。
");
    }

    void tryme()
    {
        printf("
Hi :)
");
    }

};

largeObj iReturnLargeObjects()
{
    largeObj md;


    return md;
}


int main()
{

    largeObj mdd = iReturnLargeObjects();

    mdd.tryme();

    return 0;
}

The output is

正常建筑

制模

拆除。

iii

我也想到原因。

但是,如果我替代以下一线:

largeObj mdd = iReturnLargeObjects();

iii

largeObj& mdd = iReturnLargeObjects();

产出相同,原因何在?

I mean: shouldn t there be another copy in the first case (iiiout the &)? What s the difference between these two lines and why do they behave the same?

问题回答
largeObj& mdd = iReturnLargeObjects();

你们不能把反常的“高价值”的提法束缚起来。 这是非法的C++,只允许某些具体的汇编者延期。 然而,即使这一提法是<条形码>,因此,转让是合法的,但问题的各个方面都没有变化。

您的产出没有不同,其原因就是编辑优化,称为“区域观察”。 C++中明确允许的这种优化 标准允许汇编者在某些限制范围内跳出其认为没有必要的物体,即使这样做改变了方案的属人性,因此它是一种非常不寻常的优化。

底线是:在您的复印件/搬家和骑自行车上不产生副作用,因为即使贵方的节目正确性取决于他们的需要,汇编者也可以消除这些影响。

What compiler did you use, I m willing to bet this worked because whatever compiler you used didn t zero out unused stack variables after a return, you didn t get a seg fault because its still your stack. basically md was left on the stack which you are technically still allowed to address. The difference between the two is

大 会 • 分娩;

它是整个变量的变量。

大 会 Obj& mdd = iReturnLargeObjects();

这里指的是由于 la编纂者仍然存在的一个变量。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签