*GoochRules wrote:*
I don't think you're using the right tool for the job. From what I know, smart pointers should be used when you have one object, here you (potentially) have many. I think what you really want to do here is to use a container class like stl::list or stl::vector to contain your slave objects.
If you still want to use smart pointers, I guess you could use a list of smart pointers, but there's really no point for that if your slave class is properly coded (for instance, has a ctor, copy ctor, virtual dtor, and operator=).
The slave and master classes are just example code i wrote in my mail to describe my question. I need smart pointers for memory managment in a game i'm coding and it would be a pain in the ass to code the whole thing from scratch.Shared_ptr and shared_array will save me a lot of time. *Colin Rafferty wrote:*
class Master { // ... shared_array<Slave> sl; };
Master::Master(int val) : sl(new Slave[val]) { }
Master::~Master() { // intentionally left blank! }
It works.Thanks! However i'm still having a problem understanding how i'm going to initialize the Slave array in some other function rather than in initialization, and keep the scope public for the whole Master object(excuse my terminology,i'm not english).
Of course, you may want to use scoped_array<Slave> instead of shared_array<Slave>, but that depends on what you want to do.
No.scoped_ptr and scoped_array cannot be used in STL containers and of course the pointed object cannot be shared among pointers. *Mark Storer wrote*
I'd go with scoped_array<> by default, and only go with shared_array<> if you need to share OWNERSHIP of that array. This master/slave example doesn't give enough context to make the choice apparent.
PS: There's a bug in your original code:
Master::~Master() { delete sl; }
If you use the [] version of new, you should use the [] version of delete:
Master::~Master() { delete[] sl; } Otherwise, many implementations of 'delete' will only call the destructor of the first element in the array, and may or may not free up the entire block of memory. Some implementations may actually clean everything up properly, but I wouldn't count on it if I were you.
I took for granted that you would understand the code i wrote in this mail only intended to describe my problem. I guess it's my mistake that i didn't make it clear. Anyway Mark in my real program i need to share ownership of my objects(see replies above) and you're right in what you're saying about the mistake. It's just a typo but it could be a killer bug! There are little chances your program recovers from such a bug. I'm impressed how fast people replied to my question.It's a really vivid community Sorry for the long post