Jump to content
  • Advertisement
Sign in to follow this  
MTclip

a little pointer misdirection help....

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

here is what i got... in a nutshell..
std::vector<box*> myBoxes;
box * myActiveBox;
box * activeBoxHandle;

myBoxes.push_back(new box());
myActiveBox = boxes.at(0);
activeBoxHandle = myActiveBox;


through the activeBoxHandle how can i acces the data in the original box stored in the vector.... note the data should be passed as shown above... should the Handle be a **... thanks for some help :P ................................

Share this post


Link to post
Share on other sites
Advertisement
Well, you can dereference the activeBoxHandle, or use the pointer-to-member operator ( -> ) to access it's internals. But I don't really understand what you're not able to do, I'd need a better explanation of your problem to give a better solution.

Share this post


Link to post
Share on other sites
If you want to access one of the box objects, of which you have pointers stored in the vector, all you have to do is get a reference from the vector of the pointer you want, then, de-reference the pointer, or whatever you want.


std::vector<box*> myBoxes; // stores box*

myBoxes[5]; // returns the 6th box* in the vector

*myBoxes[5].function(); // accesses the function inside the box object that
// is pointed to by the 6th box* in the vector

myBoxes[5]->function(); // exactly the same as above



Since you used the 'at' function, that leads me to believe that you don't know about the overloaded operator[] for the vector class. It does the exact same thing as the 'at' function, but makes things a whole lot more readable, since if you are up to using the vector class, you should know arrays.

Share this post


Link to post
Share on other sites
The at member function doesn't strictly do the same as operator[]. at is guaranteed to be bounds checked, whereas operator[] may or may not be bounds checked (and usually isn't). Also you can't call *myBoxes[5].function(); because the member selection operator (.) has higher precedence than the dereference operator (*). It would instead have to be (*myBoxes[5]).function();.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Also you can't call *myBoxes[5].function(); because the member selection operator (.) has higher precedence than the dereference operator (*). It would instead have to be (*myBoxes[5]).function();.


I was looking at it, saying to myself, "Should I put the brackets around it? Nah, it won't matter, they'll know what I mean. It won't be a problem."

:D

Share this post


Link to post
Share on other sites
wellthanks for the help .... but i knew that...
i prefer to use the .at() so that it can be more easily idetified as an at..

i guess i did not write the question well enough

though i found a solution i will re address the question


std::vector<box*> myBoxes;
box * myActiveBox;
box * activeBoxHandle;

myBoxes.push_back(new box());
myActiveBox = boxes.at(0);
activeBoxHandle = myActiveBox; // <----- where this points to myActivebox
// not myBoxes
////////// my solution //////////////////
std::vector<box*> myBoxes;
box * myActiveBox;
box ** activeBoxHandle;

myBoxes.push_back(new box());
myActiveBox = boxes.at(0);
activeBoxHandle = &myActiveBox
(*m_pActiveTextbox)-> doSomething() // <------ used to access myBoxes
// through myActiveBox






doing it this way allows me to check if myActiveBox == NULL
like this..

if((*activeBoxHandle) == NULL)

the reason for this is becasue the varibales are all in separate classes...
i suppose that my question should be more aperent now...
is there are way to do that with out using " box ** "

anyways thanks ;P

Share this post


Link to post
Share on other sites
Hmm... so basically, you want the class with "myActiveBox" to be able to set the pointer to NULL and have your "activeBoxHandle" variable somewhere else automatically know that you changed "myActiveBox" to NULL?

Sounds like your way is the simplest. Depending on when you create the "activeBoxHandle" variable, you could do this:


class class1
{
public:
std::vector<box*> myBoxes;
box *myActiveBox;
};

class class2
{
box *&activeBoxHandle;

public:
class2(class1 *c1) : activeBoxHandle(c1->myActiveBox)
{
}

// or...
void somefunction(box *myActiveBox)
{
box *&activeBoxHandle = myActiveBox;
// but this isn't as useful.
}
};






If you do this, your "activeBoxHandle" variable can be used exactly the same way as the "myActiveBox" it was told to point to, and will become NULL when "myActiveBox" becomes NULL:


box *myActiveBox = new box();
box *&activeBoxHandle = myActiveBox;

activeBoxHandle->DoSomething();
// same as
myActiveBox->DoSomething();

myActiveBox = NULL;
// activeBoxHandle will now also be NULL.


The thing about reference variables is that you can ONLY set what they're pointing at a) when you declare the variable, or b) in the init-list of a constructor.

Some things to think about:

- If you use the class1 and class2 example above, and free the instance to class1, and then class2 tries to access "activeBoxHandle", it will be referencing a "myActiveBox" that does not exist any more. This will probably cause crashes.

- The underlying rules are exactly the same. A *& is the same thing as a ** except you don't have to do (*activeBoxHandle)->DoSomething(). The compiler ends up turning it into that though. There's no extra safety features preventing you from crashing (like in the previous note).

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!