English 中文(简体)
制片人,为什么按价值功能进行回报
原标题:Copy constructor, why in return by value functions

附录:

class A
{
    A(A& foo){ ..... }
    A& operator=(const A& p) {  }
}

...
A lol;
...

A wow(...)
{

    return lol;
}

...
...
A stick;
stick = wow(...);

然后,我就在最后一行出现一个汇编错误。 但是,如果在A&之前加上一线,那就ok。

I want to know why. Where it s exactly the problem? I dont get why it should be const.

语言:C++

页: 1 我认为这改变了其相关性。 造成错误。

问题回答

我认为,你提到的问题与:

c++,对匿名(无名)变量的物体终身时间

在基本点是,在C++中,匿名雇员不能以提及方式通过,而只能通过参考。

下述法典将完全的罚款汇编成Comeau和VC9:

class A
{
public:
    A() {}
    A(A&){}
};

A lol;

A wow()
{
    return lol;
}

int main()
{
    A stick;
    stick = wow();
    return 0;
}

如果这 t与你的汇编者一起汇编,那么我怀疑你的汇编者会被打破。 如果是的话,那就意味着你应该遵守真正的法典,而不是提供类似于你所看到问题的幻灯。

电话wow在临时物体中产生,价值很高。 不能将R-数值分配给非最根本的参考资料。 由于您的复印件接受非最根本的参考资料,您不能直接通过打电话wow<>/code>的结果。 因此,添加<条码>const,以解决这个问题。 如今,影印商接受一些内容,这些内容的超额价值只能被罚款。

您的复印件是,复印件的制作人不会改变复制件的标本,因此,灯塔应当通过参考书。 这是因为,除了有具体、有文件记载的情况外,影印施工商预计会如何工作。

但是,正如他的回答中指出的那样,这本复印件的构件根本不停。 因此,尽管情况确实如此,但它可能与你的问题毫无关系。 除非有汇编者。 也许贵编人看到分两步施工,并决定通过将<代码>A 标签;标签 = wow();改为A 标签 = wow();。 但是,这只是一纸空文,因为它从完全的法典中产生错误。 但是,如果没有实际守则,就不可能说正在发生什么。 在与您的影印师讨论任何问题之前,应存在若干其他错误。

Not reproducible. 您是否失踪了违约构造者,或不愿使构造成为<条码>。

。 http://www.ideone.com/nPsHj

(注:复印件<>) 采用<代码>cv A&,与任何组合合在一起,加上一些缺省论点。 见C++标准中的[分类.copy]/2。


Edit: Interesting, g++-4.3 (ideone) and 4.5 (with -pedantic states) t has una have the codification mis, but g++-4.2 do su:

x.cpp: In function ‘int main()’:
x.cpp:19: error: no matching function for call to ‘A::A(A)’
x.cpp:7: note: candidates are: A::A(A&)

这一职能:

A wow(...) 
{  ... }

returns an object of by value.
This means it is copied back to the point where the function was called.

本条:

stick = wow(...);  

Does a copy construction on stick.
The value copied into stick is the value copied back from the function wow().
But remember that the result of the call to wow() is a temporary object (it was copied back from wow() but is not in a variable yet).

因此,我们现在看A的复印件:

A(A& foo){ ..... }

你们试图通过一个临时物体,进入一个参考参数。 不允许这样做。 临时物体只能受最轻的参照。 问题有两个解决办法:

1) Use a const reference.
2) Pass by value into the copy constructor.

不幸的是,如果你使用解决办法(2),那么当你成为循环依赖者时,你就会 st。 凭价值通过,需要使用复印件,以便你进入无限的休息室。 因此,你的解决办法是用粗略的参考。





相关问题
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?