Connecting Tech Pros Worldwide Forums | Help | Site Map

Seeking design pattern..

Søren Johansen
Guest
 
Posts: n/a
#1: Jul 19 '05
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



David White
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Seeking design pattern..


"Søren Johansen" <soerenjohansen171@hotmail.com> wrote in message
news:3f696c34$0$184$edfadb0f@dread11.news.tele.dk. ..[color=blue]
> Hi,
>
> I am writing a small framework for doc/view applications. For this I have[/color]
a[color=blue]
> 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[/color]
be[color=blue]
> 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.[/color]

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



Closed Thread