简体   繁体   中英

How to assign pointer of an “array of object” (C++)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM