c++ - How to properly initialize class value member? -


We say that we have it:

  square fu {public: Fu ( Const Bar & amp; B): M_bar (B) {} Private: Bar M_bar; }; Now, about efficiency, it says:  

Consider the following creator who uses the initial list to launch the member object "x": Fred :: Fred (): X (whatever) {} The most common benefit of doing this is a better performance. For example, if the expression which is the same type as the member variable "x", then in "x" The result of which the expression is created directly - the compiler does not make a separate copy of the object Is.

Should the conservator consider the value rather than the context?

Since I am using constructor initialization list, will there be any performance difference?

Finally, and most important , will there be a sense difference? For example, the new fu (bar) caller

thanks

- edit - Corrected parameter declaration as Conf reference.

I can divide it into performance and differences, as you requested:

Can the parameter be given better value instead of the manufacturer reference?

Unless it is a primitive type or a small structure, then there should be a parameter passed from the conif reference in it. Passing through the context gives you a performance difference: There is no need to copy the whole object before the program gets the constructor. For small things, however, the time saved by avoiding the copy can not be offset from the additional level of the adrenaline.

Set by ensures that you can temporarily call Constructor objects. Thus, the constructor's caller has no semantic difference for whether or not you are calling from the value or context context.

I am using constructor initialization list, so what's the performance difference?

If you do not use the initialization list, and the object you are initializing is a default constructor, it is that semantically : < / P>

  square feet {again and again; Fu (Constant and Bar Bar_) / * bar (bar) is before the beginning of the function body * / {/ *, but here it is said here, note that we can not bar (bar_) because now times Already made. That's why we can do it instead: * / bar = bar_; // Assignment operator function is called here}};  

However, if the compiler is able to see that the addition of bar to the default consignor of bar is a side effect Is not, and that the value of bar has been overwritten in the constructor's body, it can choose to completely remove this call. But we can always make life easier for the compiler and can not make that extra call.

Note that if the object you want to start does not have a default constructor, then you should want start it in the initial list. This is another meaning difference.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -