How can we create a vector of a particular data type and get it's elements' references later ?
I am new to C++ and have some understanding of pointers and references, but I am having a hard time understanding a particular pointer and reference behaviour.
In some_file.hxx:
class some_class {
protected:
std::vector<float>* _parentVector;
//rest of the body
};
In some_file.cxx under init function for class (works just like contructor):
init() {
int num = 10;
_parentVector = new std::vector<float>[num];
for(int i=0;i<num;i++)
_parentVector[i].resize(num,0);
}
In some_file.cxx inside member function:
some_class::some_func(i) {
std::vector<float>& some_var = _parentVector[i]
}
ABOVE CODE works totally fine as expected, giving me reference of a float vector from an array of vectors.
But, when I do the same with vector of strings, everything breaks inside function saying, "invalid initialization of std::basic_string& with std::basic_string
Please comment, if you want me to paste "string" usage code.
I would like to know what is the internal mechanism ? How is float working but string isn't and what pointer magic is happening ??
Thanks.
Aucun commentaire:
Enregistrer un commentaire