Returning RPointerArray<> vs * vs &
| Thu, 2006-02-09 23:04 | |
|
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 |
|






Forum posts: 364
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.
Forum posts: 723
- 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/
Forum posts: 149
True... that's actually what I ended up doing.
Oh yeah!
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
Forum posts: 1242
René Brunner
Forum posts: 723
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/
Forum posts: 149
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.
{
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
Forum posts: 723
Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/
Forum posts: 131
ref = test.getRef2(); // but ref isn't pointing to the ref2!
PS
For that matter, why not using test.getRef() directly? (assuming your accessors are inline).
Forum posts: 149
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.
For that matter, why not using test.getRef() directly? (assuming your accessors are inline).
-euroq