Questions about initialising OwnedArray

Hi all,

I’ve been trying to implement a dynamically sized array of a derived component class using ScopedPointer<ScopedPointer> and have been running into all sorts of grief. After spending a rather embarrassing amount of time failing with the ScopedPointers I proved to myself I could do it quite easily with standard pointers. I’m still not sure why I couldn’t get ScopedPointers to work, but as an alternative to my shameful non-RAII implementation I had a couple of goes at implementing using OwnedArray instead.

As it happens, I was declaring the OwnedArray as a private member in my header file like so: “OwnedArray myVar;”

When I then attempted to initialise it in my constructor code with “OwnedArray myVar = new OwnedArray;” I got a compile error and so shortened it to “OwnedArray myVar;”. This compiled OK, but resulted in a run time error which was only resolved by removing this statement (i.e. second declaration).

So my questions of utter C++ noobness:

[list]
[]Why would the second declaration cause a run time error?[/]
[]Why don’t I need to initialise the OwnedArray before adding to it?[/]
[]Is there a good reason why I couldn’t used ScopedPointer<ScopedPointer> to do a similar thing?[/]
[/list]

Thanks in advance,
Andrew

PS - I’ve spent a number of hours reading tutorials and other documentation before resorting to asking here, I’m trying not to waste your time :smiley:

This statement :

make you look like you’ve been doing Java for too long ;))))

new OwnedArray<myClass>; returns a OwnedArray<myClass>* (a pointer to an OwnedArray, i.e. it’s address)
and you’re trying to assign it to an OwnedArray (the object not it’s address), so it’s wrong.

When you write : OwnedArray<myClass> myVar;, OwnedArray default constructor is called, and myVar is correctly initialized.

Not to be rude but if I were you, I wouldn’t try coding anything in C++ before thos concepts (pointers, object, references) are not crystal clear in your mind. Have a look here for a good start : http://www.cplusplus.com/doc/tutorial/pointers/ (you can safely skip the “Pointers to functions” part at the moment)

HTH

Thanks for taking the time to respond dinaiz. You’ll notice I didn’t actually ask why “OwnedArray myVar = new OwnedArray;” didn’t work - I do understand pointers enough to understand why that was incorrect. The reason I mentioned it was to explain how I dumbly ended up with the second declaration.

As you explain, there’s no need for a new statement because myVar is an object, not a pointer to an object. I confused myself here by spending so much time trying to get the ScopedPointer approach to work. So that 's the second question answered.

As for the first question, after a few hours sleep I see that the second declaration is restricted in scope to the local function - which the compiler is OK with. Problem is that I end up adding child components from this, so the parent gets upset when I leave the function and the local goes out of scope - duh!

Thanks, Andrew

PS - not just Java, but VB & C#

[quote=“Andrew J”]Thanks for taking the time to respond dinaiz. You’ll notice I didn’t actually ask why “OwnedArray myVar = new OwnedArray;” didn’t work - I do understand pointers enough to understand why that was incorrect. The reason I mentioned it was to explain how I dumbly ended up with the second declaration.

As you explain, there’s no need for a new statement because myVar is an object, not a pointer to an object. I confused myself here by spending so much time trying to get the ScopedPointer approach to work. So that 's the second question answered.

As for the first question, after a few hours sleep I see that the second declaration is restricted in scope to the local function - which the compiler is OK with. Problem is that I end up adding child components from this, so the parent gets upset when I leave the function and the local goes out of scope - duh!

Thanks, Andrew

PS - not just Java, but VB & C#[/quote]

Oh ok, seems that I read your post too quickly. Both the fact you talked about your “utter C++ noobness” and that you wrote “OwnedArray myVar = new OwnedArray;” made me think you were more a noob than you actualy are !! Apologies :slight_smile:

Pretty noob, just not quite that noob!

Thanks again for taking the time.