Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > query on design of classes

Reply
Thread Tools

query on design of classes

 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      12-05-2007
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
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      12-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
 
Reply With Quote
 
 
 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 10:42 pm, anon <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      12-05-2007
(E-Mail Removed) wrote:
> On Dec 5, 10:42 pm, anon <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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.
 
Reply With Quote
 
pauldepstein@att.net
Guest
Posts: n/a
 
      12-06-2007
On Dec 5, 11:54 pm, anon <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Dec 5, 10:42 pm, anon <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) 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
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      12-06-2007
>> 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;

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Absurd OO Design Practice: Query Classes faceman28208@yahoo.com C++ 3 07-26-2006 09:23 PM
C++ Design Principles for Ruby - Classes of Classes. John Carter Ruby 1 08-03-2005 02:02 PM
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments