Re: C++ design question
From: Kurt (kurbylogic_at_hotmail.com)
Date: 09/28/04
- Next message: Universe: "Re: C++ design question"
- Previous message: H. S. Lahman: "Re: C++ design question"
- In reply to: Simon Elliott: "C++ design question"
- Next in thread: Robert Klemme: "Re: C++ design question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Sep 2004 10:26:02 -0700
"Simon Elliott" <Simon at ctsn.co.uk> wrote in message news:<41594030$0$94923$bed64819@news.gradwell.net>...
> I've been advised by the guys over on comp.lang.c++ to post this here
> to see if anyone can recommend a design improvement. In the following
> code example, the base class depends on an object in a derived class:
> fooBase has a reference to an object in fooDerived. It's been pointed
> out that this is potentially unsafe if bar_base_ref_ is accessed in the
> fooBase constructor.
Then don't use it in the fooBase constructor :)
Alternatively, use a two-stage construction and wrap create a factory
method to ensure it is called. Create a virtual Init method() to be
called after object has been fully constructed. virutal methods
however should not be called from the constructor either, so to ensure
that it is called you can create use a factory method to create the
object and then invoke Init():
class fooBase
{
//...
protected:
fooBase() : {}
virtual void Init() { ... }
// I like this better then passing a the ref to the base class
virtual barBase* getBarBase() = 0;
public:
//...
}
class fooDerived : public fooBase()
{
//...
protected:
fooDerived(int i1, int i2) : fooBase(), my_bar_derived_(42,43) {}
virtual barBase* getBarBase() { return &my_bar_derived_; }
virtual void Init() { ... }
public:
static fooDerived* createNew(int i1, int i2)
{ fooDerived* foo = new fooDerived(i1, i2); foo->Init(); return
foo; }
// ...
}
- Kurt
>
> Can anyone improve on this aspect of the design? Here's the code:
>
> class barBase
> {
> protected:
> int i1_;
> public:
> barBase(int i1):i1_(i1){}
> virtual ~barBase(void){};
> void SetI1(int i1){i1_ = i1;}
> };
>
> class barDerived:public barBase
> {
> protected:
> int i1_;
> int i2_;
> public:
> barDerived(int i1, int i2):i2_(i2),barBase(i1){}
> virtual ~barDerived(void){};
> };
>
> class fooBase
> {
> private:
> barBase &bar_base_ref_;
> private:
> fooBase(const fooBase &C);
> fooBase& operator=(const fooBase &C);
> public:
> fooBase(barBase &bar_base_ref):bar_base_ref_(bar_base_ref){}
> virtual ~fooBase(void){};
> void DoStuffWithBarBase(void)
> {
> bar_base_ref_.SetI1(13);
> }
> };
>
> class fooDerived:public fooBase
> {
> private:
> barDerived my_bar_derived_;
> public:
> fooDerived(int i1, int i2):fooBase(my_bar_derived_),
> my_bar_derived_(42,43){}
> virtual ~fooDerived(void){};
> };
>
> int main(int argc, char **argv)
> {
> fooDerived my_foo_derived(42,1);
> my_foo_derived.DoStuffWithBarBase();
> return 0;
> }
- Next message: Universe: "Re: C++ design question"
- Previous message: H. S. Lahman: "Re: C++ design question"
- In reply to: Simon Elliott: "C++ design question"
- Next in thread: Robert Klemme: "Re: C++ design question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|