Sign in to follow this  
XTAL256

Best way of using pointer to object in list

Recommended Posts

XTAL256    106
A few times in my project, i have wanted to store objects in a list (Qt's QList in this case, but it could be std::vector or any collection object) where i am storing the actual object and not a pointer to an object created on the heap. In other words:
//This:
List<Object> something;

//Instead of this:
List<Object*> something;

Now, in the past i have usually stored pointers to objects because the objects themselves are owned by another object, and the list is used to keep track of all these objects. So when i wanted to add an object to the list then call some methods on that object i would do:
List<Object*> theList;
...
Object* obj = new Object();
theList.add(obj);
obj->initializeOrSomething();
giveObjectToSomething(obj);    // Note: this function takes a pointer

But i have realised that in a lot of cases you want the list to contain the actual objects as that list is responsible for adding/removing/maintaining those objects. So when i want to do the above, i am not sure what the best way is:
List<Object> theList
...
theList.add(Object());
theList.last().initializeOrSomething();
giveObjectToSomething(&theList.last());

In other words, i don't know an easy way to obtain a reference to the object which i have just created. I was thinking i could create it and add it like this:
List<Object> theList
...
Object obj;
theList.add(obj);
obj.initializeOrSomething();
giveObjectToSomething(&obj);

But wouldn't that copy the object?

Share this post


Link to post
Share on other sites
TyphoidHippo    122
Not sure what language/library that List class is from with it's .add() function, but in C++ with std::list - it's as simple as this -



void MessWithInt(int* pInt)
{
*pInt *= 2;
}
void MessWithInt(int& refInt)
{
refInt *= 2;
}
int TestListPtrRefStuff ()
{
std::list<int> intList;

intList.push_back(5);

//Pointer
int* pBack = &intList.back();

MessWithInt(pBack);

//Reference, if you prefer
int& refBack = intList.back();

MessWithInt(refBack);

int shouldBe20 = intList.back();

return shouldBe20;
}



That does, in fact, return 20 over here. hope that helps

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by XTAL256
But wouldn't that copy the object?
Yes. That shouldn't be a problem though...
std::list<Object> theList;
...
theList.push_back(Object(constructor, arguments, go, here));
giveObjectToSomething(&theList.back());


It's the constructor's job for initialising an object, not some other initialisation routine. You'd need a damn good reason to do otherwise.

Share this post


Link to post
Share on other sites
Ftn    462
Quote:
In other words, i don't know an easy way to obtain a reference to the object which i have just created.

insert returns an iterator:


std::list<Object> theList;

std::list<Object>::iterator obj = theList.insert(theList.end(), Object());
obj->initializeOrSomething();
giveObjectToSomething(&*obj);

Share this post


Link to post
Share on other sites
XTAL256    106
@TyphoidHippo: That List class i used was just an example (i.e pseudo-code), it is not from any library. Besides, the code you provided is just using a primitive data type whereas i was talking about using a list of objects. And the way you access it (intList.back()) is exactly the way i said.

@iMalc: Yeah, i guess it's no problem to just copy the object, but it is unnecessary copying. And i know the constructor should initialize the object, it was a pretty poor example really. But the fact remains that i need to do something to the object.

@Ftn: That's a good idea, except that i am not using std::list, i am using QList (and i did mention that in my initial post).

So far, the best alternative is to create the object locally on the stack and copy it to the list. Since speed is not that important in most of my cases, i could do this if there is no better way.

Share this post


Link to post
Share on other sites

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