Seeking design pattern..

S

Søren Johansen

Hi,

I am writing a small framework for doc/view applications. For this I have a
document base class, CBaseDocument, that implements a number of load/save
functionalities and some other stuff.
As it is now, the constructor makes a semi-zombie object that has to be
either Load'ed or New'ed (New being a method of this class) before it can be
relied upon.

class CBaseDocument
{
public:
CBaseDocument();
virtual void Load(std::string const &filename);
virtual void New(std::string const &name);

...
};

I would like "raii-enable" the class instead so this zombie state could be
avoided. This would require a Load-constructor and a New-constructor, but
there are two problems with this. One, as you can see, Load and New are
virtual (well actually, this is simplified. Load is not virtual as the
example states but it calls virtual methods), and two, Load and New take
identical sets of parameters so I would have to use named constructors or
some other trick but this makes it difficult to inherit from the class.

Any suggestions to an approach would be greatly appreciated.

Søren
 
D

David White

Søren Johansen said:
Hi,

I am writing a small framework for doc/view applications. For this I have a
document base class, CBaseDocument, that implements a number of load/save
functionalities and some other stuff.
As it is now, the constructor makes a semi-zombie object that has to be
either Load'ed or New'ed (New being a method of this class) before it can be
relied upon.

class CBaseDocument
{
public:
CBaseDocument();
virtual void Load(std::string const &filename);
virtual void New(std::string const &name);

...
};

I would like "raii-enable" the class instead so this zombie state could be
avoided. This would require a Load-constructor and a New-constructor, but
there are two problems with this. One, as you can see, Load and New are
virtual (well actually, this is simplified. Load is not virtual as the
example states but it calls virtual methods), and two, Load and New take
identical sets of parameters so I would have to use named constructors or
some other trick but this makes it difficult to inherit from the class.

Any suggestions to an approach would be greatly appreciated.

I'm not certain I understand what you are after. Are you asking how to
create an object of some derived class and have it ready for use immediately
after construction? Other than letting some factory object or function
create the object for you and return it, I don't see how you can do it
except by having each derived class's constructor complete the
initialization. This could be done by having a constructor in each derived
class that is only called if it is the most-derived class (i.e., the
constructor with that signature is never called from a derived class), and
that constructor completes the initialization itself or calls something else
(internal or external) to complete it. Virtual functions just aren't going
to work during construction. Might the object be created on the stack or
will it always be a free-store object? I think it would help if you give
some more details and an example of how you want to use the object.

In the meantime, I suggest reading this:
http://www.jelovic.com/articles/stupid_naming.htm

DW
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top