Return object without exposing it to delete

D

Dave Rahardja

Sorry, I was thinking about auto variables, however as a practical
matter not being able to destroy what you create can be a big problem.

As a general rule, yes. However, there are times where the Abstract Factory
pattern makes this pattern desirable:

class Widget
{
public:
/* ... */
virtual void fn() = 0;

private:
virtual ~Widget();
friend class AbstractFactory;
};

class WidgetA: public Widget
{
public:
/* ... */
virtual void fn();
};

class WidgetB: public Widget
{
public:
/* ... */
virtual void fn();
};

class AbstractFactory
{
public:
/* ... */

AbstractFactory& createFactory();

// Create either WidgetA or WidgetB
Widget& createWidget();
void destroyWidget(Widget&);
};

int main()
{
AbstractFactory& factory = AbstractFactory::createFactory();

Widget& widget = factory.create();
factory.destroy(widget);

/* ... */
}

Why not just overload the delete operator in Widget? Because then Widget needs
to know about AbstractFactory, and Widget potentially needs to carry a
reference to the Abstractfactory instance.

-dr
 
D

Daniel T.

Dave Rahardja said:
As a general rule, yes. However, there are times where the Abstract Factory
pattern makes this pattern desirable.

But by making the destroy function public, you allow anyone who has a
pointer/reference to the object to delete it. The OP didn't want that to
be possible.
 
B

blackcat

Noah said:
You could use boost::shared_ptr with a nop deleter.

void noop() {}

shared_ptr<T> getT() const { return shared_ptr<T>(t, bind(&noop)); }

However, I imagine this could cause problems if you where to finish
using all the shared_ptrs that have deleters before this returned one
went out of scope. Unfortunately shared_ptr is the only one that
appears to let you override the deleter.

It also seems that shared_ptr ought to be able to be manipulated so
that it isn't deleted in the first place.

Something like


shared_ptr<std::string> get_xml_color_string( int color )
{
shared_ptr<std::string> xml_string(new std::string);

xml_string += "<color>";
xml_string += *get_color_string( color );
xml_string += "</color>";
return xml_string;
}

....
for ( ... )
{
xml_menu_string += *get_xml_color_string( ... );
next_statement;
...
}


works under g++. Evidently the temporary for the return value is
created before xml_string goes out of scope, thus keeping the
shared_ptr reference count at 1 and keeping the object from being
destroyed until next_statement begins (as verified by trying this
pattern with some terminal output in the constructor and
destructor).

This saves a boatload of temporary objects in the loop.

But I can't think of a portable way to do the same thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top