Posts

Showing posts with the label Pointers

Are There Pointers In Javascript?

Answer : No, JS doesn't have pointers. Objects are passed around by passing a copy of a reference . The programmer cannot access any C-like "value" representing the address of an object. Within a function, one may change the contents of a passed object via that reference, but you cannot modify the reference that the caller had because your reference is only a copy: var foo = {'bar': 1}; function tryToMungeReference(obj) { obj = {'bar': 2}; // won't change caller's object } function mungeContents(obj) { obj.bar = 2; // changes _contents_ of caller's object } tryToMungeReference(foo); foo.bar === 1; // true - foo still references original object mungeContents(foo); foo.bar === 2; // true - object referenced by foo has been modified You bet there are pointers in JavaScript; objects are pointers. //this will make object1 point to the memory location that object2 is pointing at object1 = object2; //this will make...

(->) Arrow Operator And (.) Dot Operator , Class Pointer

Answer : you should read about difference between pointers and reference that might help you understand your problem. In short, the difference is: when you declare myclass *p it's a pointer and you can access it's members with -> , because p points to memory location. But as soon as you call p=new myclass[10]; p starts to point to array and when you call p[n] you get a reference, which members must be accessed using . . But if you use p->member = smth that would be the same as if you called p[0].member = smth , because number in [] is an offset from p to where search for the next array member, for example (p + 5)->member = smth would be same as p[5].member = smth Note that for a pointer variable x myclass *x; *x means "get the object that x points to" x->setdata(1, 2) is the same as (*x).setdata(1, 2) and finally x[n] means "get the n-th object in an array". So for example x->setdata(1, 2) is the same as x[...