Pointer to vector
If i have vector like this:
vector vMyVector;
hove can i make it pointer, is it bossible?
example how could i get vMyVector to *pvMyVector ?
[edited by - Craazer on October 20, 2002 10:20:47 AM]
Same way you do with any other variable: the address-of operator ''&''.
vector* pvMyVector = &vMyVector
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
vector* pvMyVector = &vMyVector
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
quote:Original post by Kylotan
Same way you do with any other variable: the address-of operator '&'.
vector* pvMyVector = &vMyVector
Is that really so? i allready tryed to do that but didnt work.
see if i normally have STRUCT vector and access its variables like this:
vMyVector.x
and now if i use pointer (vector* pvMyVector = &vMyVector )
pvMyVector.x
doesnt work!
or
pvMyVector->x
doesnt work either
it doesnt work becose i start using vector class variables instead of my struct stored to vector. U know?
[edited by - Craazer on October 21, 2002 12:42:18 PM]
quote:Original post by Craazer
and now if i use pointer vector* pvMyVector = &vMyVector, pvMyVector.x doesnt work!
It''s not supposed to. Member dereference for a vector uses operator ->.
quote:
pvMyVector->x doesnt work either. it doesnt work becose i start using vector class variables instead of my struct stored to vector. U know?
No, I don''t know. If you have a vector data structure which has a member x, then a pointer to an instance of vector named pvMyVector will return the member x with the syntax pvMyVector->x. Perhaps you are doing something funky?
quote:Original post by Craazer
If i have vector like this:
vector<int> vMyVector;
There''s the problem! std::vector is a dynamic array, not a 3d vector. It doesn''t have a member named x. What you want is your own 3d vector class, something like this:
template < typename T >struct vector3{ T x, y, z;};
You can then do the following:
vector3< int > vMyVector;vector3< int > * pvMyVector = &vMyVectorpvMyVector->x = 12;
// Sorry my vector is vector<struct> vMyVector;// not vector<int> vMyVector; like i sayd, sorry. // Heres some simble code what i just write struct TT{ int x; int y;};vector<TT> vec;void main(){ vector<TT> *pvec; pvec = &vec pvec->_Ctptr; // accesses only the vector class }
So i hove can i have the pointer?
should i use template < typename T > in this one then?
[edited by - Craazer on October 21, 2002 12:56:44 PM]
quote:Original post by Craazer
vector<TT> *pvec;
Why are you using a pointer? Use a reference instead.
quote:
pvec->_Ctptr; // accesses only the vector class
You haven''t called operator[] to retrieve an element. If you want to access elements via a pointer, you should be doing this:
pvec->operator[](index).x = 10;
If you used a reference rather than pointer, you wouldn''t have to use such a warped syntax.
quote:Original post by SabreManOriginal post by Craazer
vector *pvec;
Why are you using a pointer? Use a reference instead.
I need to use pointer becose i have multible vectors wich contains stuctures but they basicly hold the same data.
so i have to choose wich vector to use at wich point.
[edited by - Craazer on October 21, 2002 1:18:54 PM]
quote:Original post by Craazer
I need to use pointer becose i have multible vectors wich contains stuctures but they basicly hold the same data.
That doesn''t rule out use of a reference.
quote:
so i have to choose wich vector to use at wich point.
You mean it needs to be reseatable? In that case, a reference won''t do, so you should consider a suitable smart pointer in preference to the dumb pointer you''re currently using. Check out the boost::shared_ptr at Boost''s website.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement