Saturday 15 June 2013

c++ - Copy Constructor Needed with temp object -


The following code works only if the copy constructor is available.

When I add a print statement (via std :: cout ) and provide a copy constructor, it is not used (I think unnecessary copying Such a compiler is doing the trick).

But both output operators and below the function plop () (I will create a floating object) I do not see the need for the copy constructor. Can anyone explain that whenever I const in context (or I do wrong I am passing) everything, then why language is needed.

  #include & lt; Iostream & gt; Class N {Public: N (Anti) {} Private: N (n cons & amp;); }; Std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & str, n const & amp; d data) {return str < & Lt; "N \ n"; } Void plop (std :: ostream & str, n const and data) {str & lt; & Lt; "N \ n"; } Int main () {std :: cout & lt; & Lt; N (1); // Copy Constructor (Line 25) Plop (std :: cout, N (1)) is required; // copy constructor NA (5); Std :: cout & lt; & Lt; a; Clap (std :: court, a); }  

Compiler:

[alpha: ~ / X] myork% g ++ -v # Using the built-in glasses. The goal: is configured with I686-apple-darwin10 /: / var / tmp / gcc / gcc-5646 ~ 6 / src / configure-disable-checking-enable-werror-prefix = / usr -mandir = / share / Man - Qualified Languages ​​= C, OBJC, C ++, Ojje-C ++ - Program-Transman-Name = / ^ [CG] [^ .-] * $ / S / $ / - 4.2 / - With- Slibdir = / Usr / lib --build = i686-apple-darwin10 --with-gxx - Add -dir = / include / c ++ / 4.2.1 - Program-prefix = i686-apple-darwin10- -host = X86_64-apple -darwin10 --target = i686-apple-darwin10
Thread Models: Pausx
GCC Version 4.2.1 (Created Apple Inc. 5646)

[alpha: ~ / A X] MyWor% G ++ T.cpp
t.cpp: function 'int main ()':
t.cpp: 10: Error: 'N :: N (Const N & amp; TDPP: 25: Error: In this context: TQP: 10: Error: 'N :: N (Constant & amp; nbsp;) is private' TDPP: 26: Error: In this context

This is a simplified version of some real code.
In real coe I have a class that has std :: auto_ptr, which means that a copy constructor who refers to a reference is not valid (without any work) and I get the signal that the copy Constructor was not available for this reason:

Change the class as well:

class N {public: n (integer) {} private: std :: auto_ptr & lt; Int & gt; Information; };

Then there is an error:

t.cpp: 25: error: call to 'n :: n (n) / p>

to

When a reference type of class rvalue for binding, The class copy creator must be accessible. For example, consider the following code:

  class A {Public: A (); Private: A (Constant A and;); // personal copy ctor}; A mea (zero); Zero Fu (Consta and;); Zero times (zero) {eff (a ()); // Error, copy CTOR FU (McA) does not have access; // error, copy ctor is not accessible a a1; Foo (A1); // OK, A1 is a new one}  

It can be amazing at first sight, especially since most popular compilers do not implement this rule () correctly Are there.

This will be fixed in C ++ 1x.


No comments:

Post a Comment