Sign in to follow this  

STD Vector Pointer

This topic is 4747 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

I have a vector of my class CTileObj and in one function I pass the vector pointer to the function. I can access the vector class methods using the standard "->" but I cannot figure out for the life of me how to access the members of the classes in the vector. Is there even a way to do that. I hope I am making myself clearer with this example: (remember, Objects is a pointer to the vector) This works: Objects->size() These DONT: Objects[i].*Active Objects[i].Active Objects[i]->Active (Active is a public variable in the CTileObj Class which is what the vector contains) I hope that it is clear what i am asking here. If you have any ideas let me know.

Share this post


Link to post
Share on other sites
Be aware that at performs bounds checking, whereas operator[] may or may not (and probably doesn't) depending on your implementation. It doesn't particularly matter which you use, but it's best to be consistent.

On another note, is there a particular reason you're passing by pointer? Passing by reference may be a better solution depending on what you're doing.

Enigma

Share this post


Link to post
Share on other sites
References are equivalent to const pointers. A reference can only ever be bound to a single object. They offer certain advantages over pointers. A reference cannot be null. Reference semantics also use the traditional syntax of operator. and not having to take the address of an object to pass it.

#include <string>

class Object
{
public:
std::string member;
}

void passByValue(Object o)
{
o.member = "changed by value";
}

void passByPointer(Object* o)
{
o->member = "changed by pointer";
}

void passByReference(Object& o)
{
o.member = "changed by reference";
}

int main()
{
Object o;
o.value = "unchanged";
passByValue(o);
// o.value still equals "unchanged"
passByPointer(&o);
// o.value now equals "changed by pointer"
passByReference(o);
// o.value now equals "changed by reference"
}



For more information try The C++ FAQ Lite.

Enigma

Share this post


Link to post
Share on other sites
Just on a note, if it is a constant pointer, doesnt that mean you CANT change the value (your pass by reference function changes the value), just an observation. Thanks, I will keep that in mind, but for now I don't want to rewrite the function to handle that, even though it wouldn't be hard. It is the thing, if it ain't broke, dont fix it. :) Cheers.

Share this post


Link to post
Share on other sites
if you pass as reference as:

void passByReference(Object& o)

you can change the value, however, if you pass as const reference:

void passByReference(const Object& o)

you can't, so you have to use the one that is best for what you want to do, it is usually a good practice to change all functions that take an object (IE somefunc(CObject object)) to take a const reference (IE somefunc(const CObject& object)), this way the behaviour inside the function is the same, but you dont have overhead of copying the whole object into the stack.

Share this post


Link to post
Share on other sites
It's equivalent to a constant pointer, not a pointer-to-const:
int main()
{
int integer = 7;
int anotherInteger = 9;
int* pointerToInteger = &integer;
const int* pointerToConstInteger = &integer;
int* const constPointerToInteger = &integer;
const int* const constPointerToConstInteger = &integer;
*pointerToInteger = 6; // OK - object pointed to is not const
pointerToInteger = &anotherInteger; // OK - pointer itself is not const
*pointerToConstInteger = 5; // ERROR - object pointed to is const
pointerToConstInteger = &anotherInteger; // OK - pointer itself is not const
*constPointerToInteger = 4; // OK - object pointed to is not const
constPointerToInteger = &anotherInteger; // ERROR - pointer itself is const
*constPointerToConstInteger = 3; // ERROR - object pointed to is const
constPointerToConstInteger = &anotherInteger; // ERROR - pointer itself is const
}


Enigma

Share this post


Link to post
Share on other sites
Ok, I misunderstood. I get it now, I think I will try that out now that I understand why exactly it is better. Thank you all and good coding.

If anyone is interested or getting this on search, for reference, here is a good article I found about the differences between references and pointers:

Pointers vs. References

[Edited by - Aurvandil on December 17, 2004 10:16:52 AM]

Share this post


Link to post
Share on other sites

This topic is 4747 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this