Thread overview
array of pointers & assignment
Apr 04, 2005
Alexander Panek
Apr 04, 2005
Tom S
Apr 04, 2005
Alexander Panek
Apr 05, 2005
Mike Parker
April 04, 2005
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
April 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 +/
April 04, 2005
On Mon, 04 Apr 2005 19:00:46 +0200, Tom S <h3r3tic@remove.mat.uni.torun.pl> wrote:

> 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 */
>

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? :o
April 05, 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.