Null OwnedArray argument


#1

May be a stupid question, but such declaration used to compile with older versions of Juce and no longer does:

void myFunction(const OwnedArray & myArray = 0);

Compiler now reports it can’t cast ‘int’ into 'const juce::OwnedArray &'
So how can I specify a default value for a function argument of type OwnedArray, so it can be omitted?


#2

OwnedArray used to have a constructor that took an integer to specify its initial capacity, but to use it as a default value would be a very misleading way of writing code! (…not to mention that I should have made that constructor explicit to prevent people misusing it in that way…)

What you need is just:


#3

OK, thanks for the tip


#4

I’m probably doing something equally wrong here as it also used to work but no longer does:

const OwnedArray& FileList = myFileChooser.getResults();

myFileChooser.getResults() returning a const Array&, there was an implicit cast from const Array& to const OwnedArray& which is no longer accepted by the compiler.
I tried making it explicit: const OwnedArray& FileList = (const OwnedArray&) myFileChooser.getResults(); and was able to compile but the content of the array isn’t transferred to the owned array properly.


#5

That really compiled!? Good job I removed the implicit cast then!

You can still use OwnedArray::addCopiesOf() to explicitly add the array to an OwnedArray, but why not just use the Array directly? It’s a much cleaner way to write the code.

That’s a good argument in favour of never using C-style casts! You’re just reinterpreting an Array object as an OwnedArray, which ain’t gonna work!


#6

Because the file list returned by the file chooser dialog was passed to a function taking an OwnedArray argument… However, it turned out that this function would work equally well with an Array, so I changed the function argument type to Array…

OK, I see…