Connecting Tech Pros Worldwide Help | Site Map

Seeking design pattern..

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:55 PM
Søren Johansen
Guest
 
Posts: n/a
Default Seeking design pattern..

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



  #2  
Old July 19th, 2005, 05:55 PM
David White
Guest
 
Posts: n/a
Default 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



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.