Skip to main content
GameDev.net gamedev.net
🔒 Locked

compare pointers in std::list

Started by Risto Hietanen Aug 1, 2006 at 7:53 AM 6 replies 1k views
Original Post
Risto Hietanen
Risto Hietanen
Hi all, Suppose the following: I have 3 instances of a class CSimpleClass: CSimpleClass* ObjA = new CSimpleClass; CSimpleClass* ObjB = new CSimpleClass; CSimpleClass* ObjC = new CSimpleClass; I then add the pointers to these classes to a list: std::list testList; testList.push_back( ObjA ); testList.push_back( ObjB ); testList.push_back( ObjC ); The list now contains copies of the pointers to the objects. Now, suppose I want to check if ObjA is present in the list, what would be the best way to do this (without using an unique id of each class)? Best Regards, Risto
ToohrVyk
ToohrVyk
std::find(testList.begin(),testList.end(),ObjA) == testList.end()

The above is true if the object ObjA cannot be found in the list.
CTar
CTar
Quote:
Original post by Risto Hietanen
Hi all,

Suppose the following:

I have 3 instances of a class CSimpleClass:

CSimpleClass* ObjA = new CSimpleClass;
CSimpleClass* ObjB = new CSimpleClass;
CSimpleClass* ObjC = new CSimpleClass;

I then add the pointers to these classes to a list:

std::list testList;

testList.push_back( ObjA );
testList.push_back( ObjB );
testList.push_back( ObjC );

The list now contains copies of the pointers to the objects.

Now, suppose I want to check if ObjA is present in the list, what would be the best way to do this (without using an unique id of each class)?

Best Regards,
Risto


Couldn't you just do this:
if( std::find( testList.begin(), testList.end(),ObjA ) != testList.end() ){  std::cout << "ObjA is in the list\n";}

Or are you talking about checking for the (*ObjA)? Then you could do this:
for( std::list<CSimpleClass*>::const_iterator iter = testList.begin();iter != testList.end();++iter){  if( **iter == *ObjA )  {    std::cout << "*ObjA is in the list\n";    break;  }}

ToohrVyk
ToohrVyk
A pointer does not "point to an address". It is an address. Now, the question is: do you want to remove any object equal to the passed object, or do you want to remove the passed object itself?

In the first case, you merely have to use std::find, which returns an iterator which you can then list.erase() to have it removed. Even simpler: you can use list.remove() to remove the object without even checking that it's there.

In the second case, you have to actually traverse the entire list to compare the objects pointed to with the passed object.




Simple clarification:

int a = 10;
int b = 10;

a and b are equal, but are not the same object. This means that you can change one without changing the other. In the following case:

int a = 10;
int *b = &a
int *c = &a

Then a, *b and *c are the same object, and b == c.
Risto Hietanen
Risto Hietanen
I want to remove the passed object from the list so that the event manager does not call the listener if it ha unregistered from the list.

So then I should do something like this:

*********************
bool UnRegister(CEventListener* listener)
{
if (listener == NULL)
{
return false;
}

for( m_It = m_Listeners.begin(); m_It != m_Listeners.end(); ++m_It)
{
if( **m_It == *listener )
{
CEventListener* tmpListener = (*m_It);
SAFE_DELETE(tmpListener);
m_Listeners.erase(m_It);
return true;
}
}
return false;
}

*******************
ToohrVyk
ToohrVyk
Your code removes the first object in the list that is equal to the passed object. Others may remain.
Risto Hietanen
Risto Hietanen

Thanks a lot for Your help ToohrVyk!
Now I think I understand how it works.

/Best Regards,
Risto

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.