'Array' in 'Array' of clases 'add' not working


#1

Am an idiot, or is it an error?

Why ‘Array::add()’ not working?

code:

class Test
{
public:
	class Serie
	{
		int n = 0;
        public:
		Array<Point<int>> values;
	};

	Test() {}

	~Test() {}

	std::vector<Serie> stdArraySeries;	// std vector
	Array<Serie> juceArraySeries;		// JUCE array

private:
	JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Test)
};

class MainContentComponent : public Component
{
public:
	MainContentComponent()
	{
            /// Testing std::vector
	   test.stdArraySeries.push_back(Test::Serie());           // Add 'Serie' to std::vector
	   test.stdArraySeries[0].values.add({ 1, 2 });            // Add 'Point' to 'values' in 'Serie'
	   int count1 = test.stdArraySeries[0].values.size();	// count1 == 1 (O.K.)

            /// Testing JUCE Array
	   test.juceArraySeries.add(Test::Serie());                // Add 'Serie' to JUCE 'Array'
	   test.juceArraySeries[0].values.add({ 1, 2 });           // Add 'Point' to 'values' in 'Serie'
           int count2 = test.juceArraySeries[0].values.size();	// count2 == 0 (??????????????????????)
	}

	~MainContentComponent() {}

private:
	Test test;
	JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainContentComponent)
};

‘count1’ is ‘1’ (O.K.)
‘count2’ is ‘0’ (bad :frowning: )


#2

The juce Array::operator[] method is different from std::vector because it returns its result by value, not by reference. So you’re modifying a temporary value.


#3

I already understand. Thanks.