I have a class that stores an array, and I need to write a method to return a pointer to this array so that other objects can get / change it.
In the old version of my program, I did this by defining a CIe-style array having a private bool* list element, and then allocating memory in the constructor (and freeing it in the destructor). The method then was very simple:
bool* MyClass::getList() { return list; }
Now I decided to rewrite the code and use std::vector<bool> instead of the classic array. The problem is that I also modified the above method as:
bool* MyClass::getList() { return &(list[0]); }
which seems to be the standard way of converting a C ++ vector to an array C. However, I cannot compile my code, I get the following error:
error: taking address of temporary [-fpermissive] error: cannot convert 'std::vector<bool>::reference* {aka std::_Bit_reference*}' to 'bool*' in return
Can someone help me with this and tell me what to do?
(I also read that another alternative is to use list.data() as a pointer, but this will only work with the latest version of C ++ compilers. I'm not sure if this is a good idea or not.)
Thanks,
source share