Returning RPointerArray<> vs * vs &

Login to reply to this topic.
Thu, 2006-02-09 23:04
Joined: 2004-05-29
Forum posts: 149
Hi all.  I am using arrays that need to be passed around to classes.  I have found that it is difficult to use references (RPointerArray<>&) because if there is a reference member variable it needs to be set during the constructor, which I can't do many times unless I pass in a reference as a parameter to the constructor.

So, one would think pointers.  Which is fine by me, but I think I'm missing out on some of the Symbian specifics.  For example, if I use pointers, that usually denotes "ownership", but alas only one class owns the array; so I have to denote for every variable in comments (not such a good idea).  Also, I have to use some ugly syntax: array->operator[](i) instead of array[ i].

On to the main question: I have found that RPointerArray<myclass> = o->GetArray(); works great.  For anotherwords, no pointers or references.  But my understanding is that this creates a copy of the array.  Which is extremly inefficient.  I know that it's not copying all of the objects in the array, just the pointers, but this is bad if I have an array of 1000 pointers!

Is this true, or does passing around a RPointerArray<> have the same efficiency as passing around a RPointerArray<>*?

-euroq


Thu, 2006-02-09 23:43
Joined: 2004-07-10
Forum posts: 364
Re: Returning RPointerArray<> vs * vs &
>> For example, if I use pointers, that usually denotes "ownership",

Hello, not sure what you mean but a class can have a pointer member variable without it having to "own" what it points to, have I misunderstood.
Fri, 2006-02-10 03:49
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Returning RPointerArray<> vs * vs &
Two things:
- It's up to you how you interpret the fact that you get a pointer to an object from someone. It's true that it's usually considered as taking over the ownership, too, but it's basically up to you how you deal with the pointer. For example, if you don't want to pass the ownership, but references don't work for some reason, then you can always pass const pointer. Okay, it limits what you can use the object for, but it's just a signal on "hey, here's a pointer, but the ownership is still mine".

- Don't use array->operator()[i], when you can use (*array)[i].

tOtE

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Fri, 2006-02-10 20:15
Joined: 2004-05-29
Forum posts: 149
Re: Returning RPointerArray<> vs * vs &
Quote from: tote
if you don't want to pass the ownership, but references don't work for some reason, then you can always pass const pointer.

True... that's actually what I ended up doing.

Quote
- Don't use array->operator()[i], when you can use (*array)[i].

Oh yeah! Cheezy I forgot.  Thanks for the advice.

Still, I'm very curious as to what happens when you have a function that returns an RPointerArray<MyClass>.  (Not a pointer or reference).  Does it actually create a copy of the entire array and then return that?

-euroq
Sat, 2006-02-11 07:33
Joined: 2005-11-20
Forum posts: 1242
Re: Returning RPointerArray<> vs * vs &
I am not fluent with every detail of the C++ language, but it seems to be that before a function can return a RPointerArray it has to get one from somewhere. I do not see any other way to "get" such an array than creating it.

René Brunner

Mon, 2006-02-13 10:28
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Returning RPointerArray<> vs * vs &
Quote from: euroq

Still, I'm very curious as to what happens when you have a function that returns an RPointerArray<MyClass>.  (Not a pointer or reference).  Does it actually create a copy of the entire array and then return that?

-euroq

If it's not a reference nor a pointer, then the data naturally gets copied.

Tote

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Thu, 2007-03-22 22:43
Joined: 2004-05-29
Forum posts: 149
Re: Returning RPointerArray<> vs * vs &
I have a variable which is a RPointerArray<>& reference that I'm getting from a class via an accessor method.  But if I try to get a reference to another object with that variable, the variable doesn't point to the original array!  It seems as if it's copied. Like,
Code:
RPointerArray<TInt>& ref = test.getRef();   // works fine
ref = test.getRef2();  // but ref isn't pointing to the ref2!

Could someone tell me if this is a standard C++ issue, or if this is a bug in the Symbian code?  The full code is below.

Code:
class TestArray
{
public:
RPointerArray<TInt> iArray;
RPointerArray<TInt> iArray2;

TestArray() {}
~TestArray() { iArray.ResetAndDestroy(); iArray2.ResetAndDestroy(); }

RPointerArray<TInt>& GetRef() { return iArray; }
RPointerArray<TInt>& GetRef2() { return iArray2; }
};

void Test()
{
TestArray test;
TInt* test1 = new TInt(1);
TInt* test2 = new TInt(2);

console->Printf(_L("Count at start: %d %d\n"), test.iArray.Count(), test.iArray2.Count());

RPointerArray<TInt>& ref = test.GetRef();
ref.Append(test1);
console->Printf(_L("ref1   (ref): %d\n"), ref.Count());
console->Printf(_L("     (orig1): %d\n"), test.iArray.Count());
console->Printf(_L("     (orig2): %d\n"), test.iArray2.Count());

ref = test.GetRef2();
console->Printf(_L("ref2 vs. original: %d %d\n"), ref.Count(), test.iArray2.Count());

ref.Append(test2);
console->Printf(_L("ref2   (ref): %d\n"), ref.Count());
console->Printf(_L("     (orig1): %d\n"), test.iArray.Count());
console->Printf(_L("     (orig2): %d\n"), test.iArray2.Count());

console->Getch();
}


The output is:

Count at start: 0 0
ref1   (ref): 1
     (orig1): 1    <-- Great! Increased the original array.
     (orig2): 0
ref2 vs. original: 0 0   <-- That's good, ref is now count 0,
                             so we should be pointing to 2nd array

ref2   (ref): 1
     (orig1): 1
     (orig2): 0   <-- WRONG! Didn't increase the original array!


Oh, and by the way, I already know that if instead of reusing the "ref" variable, it would work just fine (declare two RPointerArray<>s).  However, I still think the above should work just fine, right?  A reference is just a pointer!

-euroq
Thu, 2007-03-22 22:58
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Returning RPointerArray<> vs * vs &
What does ref.Append(test2); return, btw?

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Fri, 2007-03-23 00:54
Joined: 2006-10-07
Forum posts: 131
Re: Returning RPointerArray<> vs * vs &
Quote from: euroq
I have a variable which is a RPointerArray<>& reference that I'm getting from a class via an accessor method.  But if I try to get a reference to another object with that variable, the variable doesn't point to the original array!  It seems as if it's copied. Like,
Code:
RPointerArray<TInt>& ref = test.getRef();   // works fine
ref = test.getRef2();  // but ref isn't pointing to the ref2!
That's standard C++ and it is not the issue. It is by design. ref will still be pointing to ref1, and you copy ref2 into ref1.

PS
For that matter, why not using test.getRef() directly? (assuming your accessors are inline).
Fri, 2007-03-23 19:42
Joined: 2004-05-29
Forum posts: 149
Re: Returning RPointerArray<> vs * vs &
Quote from: sysctl
That's standard C++ and it is not the issue. It is by design. ref will still be pointing to ref1, and you copy ref2 into ref1.

Wow.  I understand that concept completely, and I'm sort of embarrassed that I didn't already know that.  I always tried to think of references as pointers that you use the . operator instead of the -> operator with, but I understand that they are also more than that.  Which is why you always have to initialize a reference.

Quote
PS
For that matter, why not using test.getRef() directly? (assuming your accessors are inline).
The problem stemmed from the fact that I had to get a reference from Dll:Tls(), which if you know is a slow call, and I didn't want to have to get it multiple times.  Since there were two arrays, I ended up reusing the same reference twice.  End the end I just made a second reference.

-euroq
  • Login to reply to this topic.