Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Craazer

Pointer to vector

This topic is 5748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 = &vMyVector;
pvMyVector->x = 12;

Share this post


Link to post
Share on other sites


    
// 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
[quote]Original 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!