Sign in to follow this  

Constructor question

This topic is 4113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, suppose you have something like this :
class cA
{
private:
	object * attrib;

public:
	cA(object * param)
	{
		attrib = param;
	}

	virtual ~cA()
	{
	}

}



cA test = new cA( new object() );


How will the attrib get deleted ? Should I consider using a boost::shared_ptr here and just delete it in the destructor ?

Share this post


Link to post
Share on other sites
If you're only ever going to be allocating new memory for object, put the call to new inside the constructor. However, I doubt that's how you're going it, so I'd say allocate the memory before calling the constructor, pass the pointer to the dynamic array to the constructor, and free the memory externally. A smart pointer is likely a good route.

That's my best guess, as I don't really know what it is this class does.

Share this post


Link to post
Share on other sites

Hey,

Thanks for the quick reply. The question was more a generic design thing.
I'm writing a procedural texture generation tool and I would use such a class to apply effects to the texture. For most effects I just pass a pointer to the texture in the Apply method and fill the effect parameters in the constructor.

For effects where I combine textures however, I would need to pass a texture to the constructor. I think I'll go for smart pointers though, just to keep all options open for the caller. A good opportunity to learn some boost, I guess.

Share this post


Link to post
Share on other sites

This topic is 4113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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