Posts

Showing posts with the label pass-by-reference

Why do i have to dereference a reference (not a pointer) to access its data (context: used as parameter in STL-algorithm)?

Why do i have to dereference a reference (not a pointer) to access its data (context: used as parameter in STL-algorithm)? I'm writing a programm that's supposed to define classes that represent a sparse matrix (matrix with few non-zero values) and then perform a few operations on a vector of these sparse matrixes. The part where i had to define the classes mostly works (1) and there also already is a working vector of the matrices ("matrixvec") itself. However now i just want to add all the matrices together using the accumulate-algorithm from the STL. This works somewhat, but not really in the way i expected. So basically i give accumulate as an initial value an external matrix called "sammelmatrix" and then accumulate adds every matrix to "sammelmatrix" (using a self-defined +operator (that works in other parts of the programm too). Using this method (instead of e.g. "sammelmatrix = accumulate(...);") i think you would need to pass sam...

In javascript how does the below code works

In javascript how does the below code works In Javascript how does the below code works. var a = { prop1: "a", prop2: "b", fun: function() { return this.prop1 + " " + this.prop2; } } var a2 = a; a.fn = "v"; a = {}; if (a === a2) { console.log(true); } else { console.log(false); } The above code prints false. But if I comment out the line a={} the value which prints on console is true. var a = { prop1: "a", prop2: "b", fun: function() { return this.prop1 + " " + this.prop2; } } var a2 = a; a.fn = "v"; //a={}; if (a === a2) { console.log(true); } else { console.log(false); } How the above code works, as Both variables(a and a2) points to the same object but when I initialized a with {} it gave false. In the first example a points to the assigned {} and a2 to the object a hold before {} was assigned to it. var a2=a; does not create an alias or r...