0

I want to store the currently selected object (selected by mouse click) and then implement methods on this object. The currently selected object is chosen from an array:

for(int i=0; i<trackList.size(); i++)
{
    trackList[i].setSelected(false);
    if((trackList[i].isClicked(x,y)) && (!trackList[i].isSelected()))
    {
        trackList[i].setSelected(true);
        currentSelected = trackList[i];
    }
}

I am new to C++ and have read up on pointers etc. but I am struggling to understand where and how they should be used. Do I need to have my currentSelected object as a pointer to whatever trackList[i] is?

Can I then implement methods on this object using the pointer reference?

Many thanks

EDIT: trackList is storing a vector of Track objects:

std::vector<interface1::Track> trackList;

And currentSelected is storing a Track object which I want to apply methods to:

interface1::Track* currentSelected;
user1356791
  • 165
  • 6
  • 16
  • You don't show any details of what trackList is storing. What is the type/class declaration? – OldProgrammer Mar 02 '13 at 16:41
  • 1
    Given information to answer your question is not enough. Maybe you should use pointer, maybe no. – masoud Mar 02 '13 at 16:42
  • From the given code sample, I can assume that you only set `currentSelected` once in the loop cycle. Could should probably (give the extent I can understand from the code) use the address of operator, i.e.: `currentSelected = &trackList[i];`. – Oliver Spryn Mar 02 '13 at 16:44

1 Answers1

1

You need to do:

 currentSelected = &(trackList[i]);

In order to assign the pointer the value of the address of trackList[i].

Another way is to use iterators, like this:

std::vector<interface1::Track> trackList;
std::vector<interface1::Track>::iterator it, currentSelected;
for (it = trackList.begin(); it != trackList.end(); it++)
{
    it->setSelected(false);
    if((it->isClicked(x,y)) && (!it->isSelected()))
    {
        it->setSelected(true);
        currentSelected = it;
    }
}

Later you can use currentSelected->setSelected(false); for both the pointer and iterator.

ruben2020
  • 1,549
  • 14
  • 24
  • Can I then call methods on the currentSelected pointer as normal to change member variable values, or do the methods have to be defined differently? – user1356791 Mar 02 '13 at 16:48
  • 1
    You need to call it like this: `currentSelected->setSelected(false);` which is deferencing a pointer. – ruben2020 Mar 02 '13 at 16:52
  • One more thing, when running my program with these changes, I get "Access violation reading location .."? – user1356791 Mar 02 '13 at 16:57
  • It just says : Unhandled exception at 0x00F04762 in Interface1.exe: 0xC0000005: Access violation reading location 0x00000054. – user1356791 Mar 02 '13 at 17:04
  • 1
    The use of currentSelected is only temporary. When currentSelected pointer goes out of scope, or if the vector is changed, then it may not be usable any more. – ruben2020 Mar 02 '13 at 17:06
  • I have currentSelected defined in global scope though? – user1356791 Mar 02 '13 at 17:07
  • Have you tried to use it as an iterator instead of a pointer? I've used it like that in my programs. – ruben2020 Mar 02 '13 at 17:09
  • Trying that I get vector iterator not deferencable? p.s. thanks for being patient – user1356791 Mar 02 '13 at 17:15
  • 1
    Probably the vector has changed, and so your iterator is no longer pointing at the right place. – ruben2020 Mar 02 '13 at 17:24
  • Please see http://stackoverflow.com/questions/759274/what-is-the-lifetime-and-validity-of-c-iterators and http://stackoverflow.com/questions/2062956/checking-if-an-iterator-is-valid – ruben2020 Mar 02 '13 at 17:32
  • So is there no way of doing what I want? The vector should change in terms of object position as it is created in my setup method. The objects member values will change but this shouldn't matter no? – user1356791 Mar 02 '13 at 17:33
  • Each time before using `currentSelected`, you can re-run the method above and update it. – ruben2020 Mar 02 '13 at 17:36
  • That would rely on the track constantly being clicked for it to remain the currentSelected as it is clicking on the track object that assigns it to currentSelected – user1356791 Mar 02 '13 at 17:40
  • 1
    Or rather, go through the vector using `it` iterator and find the selected one and assign `currentSelected = it`. – ruben2020 Mar 02 '13 at 17:43
  • Doesn't seem to work. Oh well. Thanks so much for your help anyway, much appreciated. – user1356791 Mar 02 '13 at 17:49