digitalmars.D - array of pointers & assignment
- Alexander Panek (23/23) Apr 04 2005 Hello,
- Tom S (13/28) Apr 04 2005 In D you'd declare an array of pointers to Node using this syntax:
- Alexander Panek (9/24) Apr 04 2005 That was my concern, to have only references to objects and not copied
- Mike Parker (4/8) Apr 04 2005 Just be aware that classes are passed by reference, but structs are
Hello, I`ve got a problem with the assignment of a pointer inside an array of pointers: class A { Node[] *Nodes; void foo(Node xyz) { int i = 0; Nodes[i] = &xyz; } } class Node { ... some data ... } Dmd gives me this error: xml.d(350): cannot implicitly convert expression (#this.Nodes[i]) of type Node * to xml.Node[] .. and I don`t really get what`s wrong here. "&xyz" should give me the address of the object "xyz" , so it should be a pointer, shouldn`t it? Regards, Alex -- huh? did you say something? :o
Apr 04 2005
Alexander Panek wrote:Hello, I`ve got a problem with the assignment of a pointer inside an array of pointers: class A { Node[] *Nodes; <snip>In D you'd declare an array of pointers to Node using this syntax: Node*[] nodes; /* http://digitalmars.com/d/arrays.html */ But generally you can get away with: Node[] nodes; And that will be an array of references to Node instances. /* nodes[i] = xyz; won't create a copy of the object but a copy of the reference */Dmd gives me this error: xml.d(350): cannot implicitly convert expression (#this.Nodes[i]) of type Node * to xml.Node[] .. and I don`t really get what`s wrong here. "&xyz" should give me the address of the object "xyz" , so it should be a pointer, shouldn`t it?Yes, and it is a pointer :) That's what dmd is saying: "(...) cannot implicitly convert expression (#this.Nodes[i]) of type Node * (...)" -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Apr 04 2005
On Mon, 04 Apr 2005 19:00:46 +0200, Tom S <h3r3tic remove.mat.uni.torun.pl> wrote:Alexander Panek wrote:That was my concern, to have only references to objects and not copied objects at all. Thanks for your help, gonna try that :). Regards, Alex -- huh? did you say something? :oHello, I`ve got a problem with the assignment of a pointer inside an array of pointers: class A { Node[] *Nodes;> <snip> In D you'd declare an array of pointers to Node using this syntax: Node*[] nodes; /* http://digitalmars.com/d/arrays.html */ But generally you can get away with: Node[] nodes; And that will be an array of references to Node instances. /* nodes[i] = xyz; won't create a copy of the object but a copy of the reference */
Apr 04 2005
Alexander Panek wrote:On Mon, 04 Apr 2005 19:00:46 +0200, Tom S <h3r3tic remove.mat.uni.torun.pl> wrote:That was my concern, to have only references to objects and not copied objects at all.Just be aware that classes are passed by reference, but structs are passed by value. So you don't need to use pointers for classes, but you would for structs to avoid copying.
Apr 04 2005