c++ - Compiler-generated copy/assignment functions for classes with reference and const members -


The book I am reading says that when there is a member in your class that is a reference or a const. , The copy maker or assignment operators generated by the compiler will not work. For example, #include & lt; Iostream & gt; #include & lt; String & gt; using namespace std; Class text block {public: text block (string str): s (str) {cout & lt; & Lt; "Constructor is being called" & lt; & Lt; Endl; } String & amp; S; }; Int main () {text block P ("af"); Textbox q (p); Q = p; Cout & lt; & Lt; "Q's" & lt; & Lt; q.s & lt; & Lt; Endl; Return (0); }

According to my book, both lines are TextBlock q (p); and q = p; should return compiler errors, but using the G ++ compiler for Linux, I only line q = p; is getting an error, when I comment on it, it works fine and compile the code is output for the correct SQL, so it is apparently compiled by the compiler-generated copy constructor Copying. When I call line string & amp; C; to const string << / code>.

Has some changes made in C ++ that allow the copy constructor to automatically generate context and context for the object, but not the assignment operator? Or maybe I do not understand the book correctly? any idea?

The book is wrong A consultant member or reference member will disrupt the generation of the default copy assignment operator, but the compiler Does not stop making copy constructor

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -