Re: template method gof pattern

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Arnaud Debaene (adebaene_at_club-internet.fr)
Date: 12/08/04


Date: Wed, 8 Dec 2004 19:57:23 +0100

John wrote:
<snip>

You should :
- Call CTest constuctor before CStrategyBase constructor. Although not
strictly necessary in your case, it is a better style to pass a complete,
initialized CTest to CStrategyBase constructor.
- Use an explicit cast to remove the ambiguity about the constructor
overload to call.

Also, if you are not sure about using inheritance vs aggregation for
embedding a CTest into CCompositor, perhaps you should choose private
inheritance instead of public inheritance to avoid the "is-a" relationship
(Liskov principle). However, I cannot say for sure since I don't understand
what is the semantic of your classes.

Arnaud
MVP - VC

Corriged version :

class CTest
{
private:
   int i_;

public:
   int getI() { return i_; }
   void setI(const int rhs) { i_ = rhs; }

   // Construction ------------------------------------------------

   // Copying - no copy allowed, no assignment allowed
private:
   CTest(const CTest& b) {}
   CTest& operator=(const CTest& a) { return *this; }

   // Construction
public:
   CTest() : i_(1) {}
   ~CTest() {}

};

class CStrategyBase
{
private:
   int k_;

protected:
   CTest& test_;

public:
   int getK() { return k_; }
   void setK(const int rhs) { k_ = rhs; }

   // Construction --------------------------------------------------

   // Copying - no copy allowed, no assignment allowed
private:
   CStrategyBase(const CStrategyBase& b) : test_(b.test_) {}
   CStrategyBase& operator=(const CStrategyBase& a) { return *this; }

   // Construction
public:
   CStrategyBase(CTest& t) : test_(t), k_(1) {}
   virtual ~CStrategyBase() {}

};

class CCompositor : private CTest, public CStrategyBase
{
private:
   int m_;

public:
   int getM() { return m_; }
   void setM(const int rhs) { m_ = rhs; }

   // Construction -------------------------------------------------

   // Copying - no copy allowed, no assignment allowed
private:
   CCompositor(const CCompositor& b) : CStrategyBase(b.test_) {}
   CCompositor& operator=(const CCompositor& a) { return *this; }

   // Construction
public:
   CCompositor() : m_(1), CStrategyBase(*static_cast<CTest*>(this)) {}
//call CTest constructor before CStrategyBase constructor.
   virtual ~CCompositor() {}
 



Relevant Pages

  • template method gof pattern
    ... below) is that the ctor of the CCompositor invokes the copy ctor on the ... If CCompositor aggregates a CTest and passes _it_ to CStrategy in the ... int getI() ...
    (microsoft.public.vc.language)
  • Re: implementing composition
    ... > int nn; ... guarantee the lifetime of Array elements, ... The first design stores copies of CTest ...
    (comp.lang.cpp)
  • Re: AspectJ: solution to Javas repetitiveness?
    ... public static void main(String[] args) ... private String myString; ... public TestChild(float ftest, char ctest) ...
    (comp.lang.java.programmer)
  • Re: help: class compile error
    ... > defines a global int i. so I can use ... > class CTest ... reference, unless they somehow _change_ the arguments. ... take its argument by a _const_ reference (unless it intends to change ...
    (comp.lang.cpp)
  • Re: ThisForm.Release()
    ... PRIVATE cTest ... Variable im Programm darüber (also das Programm, ... Dann nimmt man PRIVATE. ... PARAMETERS oder LPARAMETERS? ...
    (microsoft.public.de.fox)