Seeking design pattern..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Søren Johansen

    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::strin g 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

    #2
    Re: Seeking design pattern..

    "Søren Johansen" <soerenjohansen 171@hotmail.com > wrote in message
    news:3f696c34$0 $184$edfadb0f@d read11.news.tel e.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::strin g 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:


    DW



    Comment

    Working...