By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,796 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,796 IT Pros & Developers. It's quick & easy.

Seeking design pattern..

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Søren Johansen" <so***************@hotmail.com> wrote in message
news:3f*********************@dread11.news.tele.dk. ..
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

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.