C++: trying to avoid copies and pointers... no luck
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++: trying to avoid copies and pointers... no luck
Well, I am writing a code to perform a 2D random walk: there are sites
struct Site {
index;
species;
}
and a lattice
class Lattice {
public:
// ctor, dtor, functions, methods, blah blah blah
int randomWalk(const int position);
private:
// more stuff
vector<Site> _sites;
}
the idea is to move a species from _sites.at(0) to _sites.at(1), like
randomWalk(pos) {_sites.at(pos+1).species = _sites.at(pos).species; _sites.at(pos).species = 0; return pos+1;}
but it is a bit more complicated so I would like to do something similar to
... { Site origin = _sites.at(pos); Site destination = _sites.at(pos+1); // the rest }
this obviously does not work because the species is moved on the copies and not on the lattice.
On the first version, I was using a vector of pointers to "Site"s
vector<Site*> _sites;
and it was working because I was not making a copy but using the address of a site really into the lattice.
Now, I can make it work if I update the lattice, since the copies have the correct index.
So it is not like I cannot fix it.
BUT, the question is, is there an elegant way to do what I want, without making unnecessary copies and using a vector of objects instead of a vector of pointers?
Note that this is a *random* walk, so the destination is actually not at pos+1, but needs to be calculated in the real program. And I have boundary conditions, so the arrival can be at pos+(x dimension of the lattice) - (size of the lattice) or any non trivial shift from the origin.
I'd really appreciate a nice solution before I go back to the vector of pointers...
If you have the boost libraries, using a boost::shared_ptr is perfect for stuff like this.
typedef boost::shared_ptr<Site> SitePtr;
Then make your vector:
vector<SitePtr> _sites;
Now it will work the same as a vector<Site *>, but you don't have to worry about deleting the pointers when you are done with them, and boost::shared_ptr is safe to use in containers unlike auto_ptr.
Just the shared_ptr object get copied. They use reference counting to know when they are no longer used, and delete the objects.
Thank you for your answers, so I will go back to the vector of pointers, then. I'll have a look at boost::shared_ptr too but in principle, the size should not change. And yes, the size will be significant, like 128*128 or 256*256, maybe.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.