Reputation: 201
I am a beginner in C++ that confused over dynamic array. I found a way to do it in this site, but I can't seem to make it work for "array of object" in an object. It always break when I read the Rec variable.
This is the class definition:
class AlcXIO {
private:
AlcX_IO_Record* Rec[1];
int _Count;
public:
int count();
void Init(CL_DisplayWindow window);
void AddInput(int IO_ID);
AlcX_IO_Record* GetRec(int RecID);
void on_input_down(const CL_InputEvent &key, const CL_InputState &state);
void on_input_up(const CL_InputEvent &key, const CL_InputState &state);
};
The AddInput() function:
void AlcXIO::AddInput(int IO_ID) {
size_t newSize = this->_Count +1;
AlcX_IO_Record* newArr = new AlcX_IO_Record[newSize];
memcpy( newArr, Rec, _Count * sizeof(AlcX_IO_Record) );
_Count = newSize;
delete [] Rec;
Rec[0] = newArr;
}
I aware that I'm probably wrong on: Rec[0] = newArr
But changing it to Rec = newArr
gives me an error: "expression must be a modifiable lvalue"
Any solution is welcome, thank you.
Upvotes: 0
Views: 144
Reputation: 41958
Remove the [1] from the declaration, you're using it as a pointer, not an array of pointers as it is declared now. The error stems from trying to overwrite the fact that you declared it as an array - the type cannot be changed programmatically at runtime, even though it's technically compatible.
Upvotes: 1