Jump to content
  • Advertisement
Sign in to follow this  
Aurvandil

STD Vector Pointer

This topic is 4965 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.*Active Objects.Active Objects->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
Advertisement
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
Sign in to follow this  

  • 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!