c++ - assignment operator by value not compiling with explicit copy constructor -


I want to understand that the correct parameter type should be used in a clear copy constructor (Co.).

As defined below, the assignment code will not compile, using "clear". Generates assignment lines in the main: 'No matching constructor for initialization of CCAT'

Removing "clear" from the first copy constructor fixes the problem, but I do not understand Why

Is the compiler obviously doing some subtle underlying molding?

  Class CCat: Public Candidate {Public: Clear CCAT (string name, UIT noelgs, bull fur, bull house, string breed); Clear CCAT (Constant CCuit and Oktat): Canmil (Old Cat) {std :: cout & lt; & Lt; "\ NCCat :: CCAT (Const. CCAT and Old Cat) \ n"; } Clear CCAT (CCAT and Old Cat): Canmil (Old Cat) {std :: cout & lt; & Lt; "\ Nexplicit CCAT :: CCAT (CCAT and old cats) \ n"; } ~ Cecity (); CCAT & amp; Operator = (ccat old cats) {//. Assignment content return * This; }}; Int main (int argc, const char * argv []) {CCat * cat1 = new cecity (string ("waggi"), 4, true, string ("tabby")); CCAT * Cat 2 = New CCET (string ("Tibbles"), 4, true, true, string ("Tom")); CCAT Template Cat (* Cat 1); CCAT tempCat2 (* cat2); Std :: cout & lt; & Lt; "CCT tempCat2 (* cat2); \ n"; Const CCAT & amp; TempCat3 = * cat2; TempCat = tempCat3; // First C / Constant will not compile without explicitly removing tempCat = CCat (* cat2); // Before the C / Constant will not compile without removing explicit from tempCat = tempCat2; // will not compile without clearly removing C / Concerrer Return 0; }  

The assignment operator (using the value) forces the copy constructor to use, however, when used explicitly, a perfect match can not be found. So when clear is removed then what is the conversion that performs the compiler and how can I write a mail copy constructor?

assignment

  tempCat = tempCat3  

is checked by the overload resolution and rewritten as

  tempCat.operator = (tempCat3))  

overload Supports a mail member function to call resolution. It looks for your copy assignment operator:

  CCAT & amp; Operator = (old CACT cut)  

parameter old CAT to be started with the argument, tempCat3 . The correct word is actually copy-initialization .

is in the form Start

  T x = b; As well as the logic is passing, [...]  is called copy-initialization .   

copy-initialization only works with non-obvious creators:

For copy-initialization, the candidate function works with all the converter constructors of that class ( 12.3.1).

(also known as converter):

function-specifier clear unauthorized constructor of its class types Specifies a conversion from the type of its parameter, such constructor is called the converter constructor .

So if you have const as a reference parameter, it is not a constructor constructor, copy-initialization does not work And the parameter can not be started with the start - which the compiler tells us, which also does not match the built-in constructor. This same applies to other lines in the main .

If clear is removed, initialization work is fine.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -