query on design of classes

Discussion in 'C++' started by pauldepstein@att.net, Dec 5, 2007.

  1. Guest

    I am working with a class which has a constructor which takes several
    parameters:
    class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    YetAnotherType C) {} // ....}

    I want to amend MyClass slightly so that it takes more parameters:
    class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    YetAnotherType C, AddOneMoreType D) {} // ....}
    I am told not to do that because MyClass should not be changed and
    because adding more parameters is seen as unwieldy. I am told to
    derive a class from MyClass and to add the extra parameters such as D
    of type AddOneMoreType etc. to the derived class. This must be quite
    a common thing to do but I'm not sure how to design this.

    Something like class MyNewClass: public MyClass{ public:
    MyNewClass(AddOneMoreType D){} } but this doesn't work because this
    doesn't reflect the fact that members of MyNewClass also take
    parameters A, B and C from the base class.

    Another idea is class MyNewClass: public MyClass{ public:
    MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
    AddOneMoreType D) {} } but that leads to the objection that using
    too many parameters in the constructor is seen as unwieldy.

    Thank you for any suggestions.

    Paul Epstein
    , Dec 5, 2007
    #1
    1. Advertising

  2. anon Guest

    wrote:
    > I am working with a class which has a constructor which takes several
    > parameters:
    > class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    > YetAnotherType C) {} // ....}
    >
    > I want to amend MyClass slightly so that it takes more parameters:
    > class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    > YetAnotherType C, AddOneMoreType D) {} // ....}
    > I am told not to do that because MyClass should not be changed and
    > because adding more parameters is seen as unwieldy. I am told to
    > derive a class from MyClass and to add the extra parameters such as D
    > of type AddOneMoreType etc. to the derived class. This must be quite
    > a common thing to do but I'm not sure how to design this.
    >
    > Something like class MyNewClass: public MyClass{ public:
    > MyNewClass(AddOneMoreType D){} } but this doesn't work because this
    > doesn't reflect the fact that members of MyNewClass also take
    > parameters A, B and C from the base class.
    >
    > Another idea is class MyNewClass: public MyClass{ public:
    > MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
    > AddOneMoreType D) {} } but that leads to the objection that using
    > too many parameters in the constructor is seen as unwieldy.


    class MyNewClass: public MyClass
    {
    public:
    MyNewClass( SomeType A,
    SomeOtherType B,
    YetAnotherType C,
    AddOneMoreType D ) : MyClass( A, B, C )
    {}
    }

    is the correct way, but some classes are just not meant to be used as
    base classes. What if MyClass has no virtual destructor?
    anon, Dec 5, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 5, 10:42 pm, anon <> wrote:
    > wrote:
    > > I am working with a class which has a constructor which takes several
    > > parameters:
    > > class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    > > YetAnotherType C) {} // ....}

    >
    > > I want to amend MyClass slightly so that it takes more parameters:
    > > class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    > > YetAnotherType C, AddOneMoreType D) {} // ....}
    > > I am told not to do that because MyClass should not be changed and
    > > because adding more parameters is seen as unwieldy. I am told to
    > > derive a class from MyClass and to add the extra parameters such as D
    > > of type AddOneMoreType etc. to the derived class. This must be quite
    > > a common thing to do but I'm not sure how to design this.

    >
    > > Something like class MyNewClass: public MyClass{ public:
    > > MyNewClass(AddOneMoreType D){} } but this doesn't work because this
    > > doesn't reflect the fact that members of MyNewClass also take
    > > parameters A, B and C from the base class.

    >
    > > Another idea is class MyNewClass: public MyClass{ public:
    > > MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
    > > AddOneMoreType D) {} } but that leads to the objection that using
    > > too many parameters in the constructor is seen as unwieldy.

    >
    > class MyNewClass: public MyClass
    > {
    > public:
    > MyNewClass( SomeType A,
    > SomeOtherType B,
    > YetAnotherType C,
    > AddOneMoreType D ) : MyClass( A, B, C )
    > {}
    >
    > }
    >
    > is the correct way, but some classes are just not meant to be used as
    > base classes. What if MyClass has no virtual destructor?- Hide quoted text -
    >
    > - Show quoted text -


    Thanks a lot. I wanted to avoid long constructors. (In reality I
    would be adding parameters D, E etc to MyNewClass.) Perhaps I could
    accomplish this by means of just adding a pointer. I was hoping there
    was some syntax available that looked a bit like class MyNewClass:
    public MyClass
    > {
    > public:
    > MyNewClass( AddOneMoreType D ) : MyClass( A, B, C )
    > {}
    >
    > }


    I know this is illegal though (hence my question). Thanks again for
    your solution.

    Paul Epstein
    , Dec 5, 2007
    #3
  4. anon Guest

    wrote:
    > On Dec 5, 10:42 pm, anon <> wrote:
    >> wrote:
    >>> I am working with a class which has a constructor which takes several
    >>> parameters:
    >>> class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    >>> YetAnotherType C) {} // ....}
    >>> I want to amend MyClass slightly so that it takes more parameters:
    >>> class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    >>> YetAnotherType C, AddOneMoreType D) {} // ....}
    >>> I am told not to do that because MyClass should not be changed and
    >>> because adding more parameters is seen as unwieldy. I am told to
    >>> derive a class from MyClass and to add the extra parameters such as D
    >>> of type AddOneMoreType etc. to the derived class. This must be quite
    >>> a common thing to do but I'm not sure how to design this.
    >>> Something like class MyNewClass: public MyClass{ public:
    >>> MyNewClass(AddOneMoreType D){} } but this doesn't work because this
    >>> doesn't reflect the fact that members of MyNewClass also take
    >>> parameters A, B and C from the base class.
    >>> Another idea is class MyNewClass: public MyClass{ public:
    >>> MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
    >>> AddOneMoreType D) {} } but that leads to the objection that using
    >>> too many parameters in the constructor is seen as unwieldy.

    >> class MyNewClass: public MyClass
    >> {
    >> public:
    >> MyNewClass( SomeType A,
    >> SomeOtherType B,
    >> YetAnotherType C,
    >> AddOneMoreType D ) : MyClass( A, B, C )
    >> {}
    >>
    >> }
    >>
    >> is the correct way, but some classes are just not meant to be used as
    >> base classes. What if MyClass has no virtual destructor?- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Thanks a lot. I wanted to avoid long constructors. (In reality I
    > would be adding parameters D, E etc to MyNewClass.) Perhaps I could
    > accomplish this by means of just adding a pointer. I was hoping there
    > was some syntax available that looked a bit like class MyNewClass:
    > public MyClass
    >> {
    >> public:
    >> MyNewClass( AddOneMoreType D ) : MyClass( A, B, C )
    >> {}
    >>
    >> }

    >


    IMO this is better then inheritance:

    public MyClass
    {
    public:
    MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
    {}

    private:
    MyClass C;
    AddOneMoreType D;

    }

    PS Better to pass C and D via reference or a pointer - depends what
    types they are.
    anon, Dec 5, 2007
    #4
  5. Guest

    On Dec 5, 11:54 pm, anon <> wrote:
    > wrote:
    > > On Dec 5, 10:42 pm, anon <> wrote:
    > >> wrote:
    > >>> I am working with a class which has a constructor which takes several
    > >>> parameters:
    > >>> class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    > >>> YetAnotherType C) {} // ....}
    > >>> I want to amend MyClass slightly so that it takes more parameters:
    > >>> class MyClass {public: MyClass(SomeType A, SomeOtherType B,
    > >>> YetAnotherType C, AddOneMoreType D) {} // ....}
    > >>> I am told not to do that because MyClass should not be changed and
    > >>> because adding more parameters is seen as unwieldy. I am told to
    > >>> derive a class from MyClass and to add the extra parameters such as D
    > >>> of type AddOneMoreType etc. to the derived class. This must be quite
    > >>> a common thing to do but I'm not sure how to design this.
    > >>> Something like class MyNewClass: public MyClass{ public:
    > >>> MyNewClass(AddOneMoreType D){} } but this doesn't work because this
    > >>> doesn't reflect the fact that members of MyNewClass also take
    > >>> parameters A, B and C from the base class.
    > >>> Another idea is class MyNewClass: public MyClass{ public:
    > >>> MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
    > >>> AddOneMoreType D) {} } but that leads to the objection that using
    > >>> too many parameters in the constructor is seen as unwieldy.
    > >> class MyNewClass: public MyClass
    > >> {
    > >> public:
    > >> MyNewClass( SomeType A,
    > >> SomeOtherType B,
    > >> YetAnotherType C,
    > >> AddOneMoreType D ) : MyClass( A, B, C )
    > >> {}

    >
    > >> }

    >
    > >> is the correct way, but some classes are just not meant to be used as
    > >> base classes. What if MyClass has no virtual destructor?- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > Thanks a lot. I wanted to avoid long constructors. (In reality I
    > > would be adding parameters D, E etc to MyNewClass.) Perhaps I could
    > > accomplish this by means of just adding a pointer. I was hoping there
    > > was some syntax available that looked a bit like class MyNewClass:
    > > public MyClass
    > >> {
    > >> public:
    > >> MyNewClass( AddOneMoreType D ) : MyClass( A, B, C )
    > >> {}

    >
    > >> }

    >
    > IMO this is better then inheritance:
    >
    > public MyClass
    > {
    > public:
    > MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
    > {}
    >
    > private:
    > MyClass C;
    > AddOneMoreType D;
    >
    > }
    >
    > PS Better to pass C and D via reference or a pointer - depends what
    > types they are.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks anon. Yes I also prefer this. This is exactly the kind of
    thing I was looking for. I was surprised at the first line of your
    code -- public MyClass I expected to see class MyClass
    Is this an error caused by inadvertently lapsing into java or am I
    missing something?

    Thanks again.

    Paul Epstein
    , Dec 6, 2007
    #5
  6. anon Guest

    >> public MyClass
    >> {
    >> public:
    >> MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
    >> {}
    >>
    >> private:
    >> MyClass C;
    >> AddOneMoreType D;
    >>
    >> }
    >>
    >> PS Better to pass C and D via reference or a pointer - depends what
    >> types they are.- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Thanks anon. Yes I also prefer this. This is exactly the kind of
    > thing I was looking for. I was surprised at the first line of your
    > code -- public MyClass I expected to see class MyClass
    > Is this an error caused by inadvertently lapsing into java or am I
    > missing something?


    Stupid copy/paste.

    Should be:

    class MyNewClass
    {
    public:
    MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
    {}

    private:
    MyClass C;
    AddOneMoreType D;

    }
    anon, Dec 6, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    1
    Views:
    973
    Marina Levit [MVP]
    Jan 30, 2006
  2. Anonymous
    Replies:
    0
    Views:
    1,440
    Anonymous
    Oct 13, 2005
  3. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    780
    David Gordon
    May 18, 2005
  4. Replies:
    3
    Views:
    294
    Default User
    Jul 26, 2006
  5. John Carter
    Replies:
    1
    Views:
    115
Loading...

Share This Page