c++ - Method questions - What changes the value and what doesn't? What's invalid? -


I have homework questions

What has been done after these methods and why? If the method is invalid, then explain why

  template & lt; Typename T & gt; Zero reset 1 (Mathequate & lt; T & gt; v) {v [0] = 0;} Template & lt; Typename T & gt; Zero Reset 2 (MathVector & lt; T & gt; & amp; v) {v [0] = 0;} Template & lt; Typename T & gt; Zero Reset 3 (Constant Math Gator & lt; T & gt; & amp; v) {v [0] = 0;}  

My previous experience tells me that the first vector is unchanged Because it is not being passed from the price, but by copy, the second will change properly, and the third will be invalid because the parameter specifies.

Although I'm pretty sure that the c ++ arrays are always passed by value because it does not automatically call a copy maker or anything, I suspect it's true about this vector May be, but I'm not really sure. So if this was the case then the first vector will be changed and the second one would be invalid because you are trying to work on the pointer of the vector, which is not understood? I'm not really here

  template & lt; Typename T & gt; Void reset1 (MathVector & lt; T & gt; v) {v [0] = 0;}  

This vector passes by value , so vector Any modification in V will be local, and after the completion of the function the original vector will be unchanged.

  template & lt; Typename T & gt; Zero Reset 2 (Mathequator & lt; T & gt; & amp; v) {v [0] = 0;}  

This vector passes from context So the first element is changing, in fact the original vector will be modified, the first element has been changed to 0.

  template & lt; Typename T & gt; Zero Reset 3 (Constant Mathworks & lt; T & gt; & amp; v) {v [0] = 0;}  

The vector here has also been passed from context , But this constant . This means that you can not try to modify the vector, it should at least produce compiler warning.


Comments