PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > query on design of classes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
query on design of classes

Réponse
 
LinkBack Outils de la discussion
Vieux 05/12/2007, 14h33   #1
pauldepstein@att.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut query on design of classes

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
  Réponse avec citation
Vieux 05/12/2007, 14h42   #2
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query on design of classes

pauldepstein@att.net 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?
  Réponse avec citation
Vieux 05/12/2007, 14h54   #3
pauldepstein@att.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query on design of classes

On Dec 5, 10:42 pm, anon <a...@no.no> wrote:
> pauldepst...@att.net 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
  Réponse avec citation
Vieux 05/12/2007, 15h54   #4
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query on design of classes

pauldepstein@att.net wrote:
> On Dec 5, 10:42 pm, anon <a...@no.no> wrote:
>> pauldepst...@att.net 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.
  Réponse avec citation
Vieux 06/12/2007, 00h00   #5
pauldepstein@att.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query on design of classes

On Dec 5, 11:54 pm, anon <a...@no.no> wrote:
> pauldepst...@att.net wrote:
> > On Dec 5, 10:42 pm, anon <a...@no.no> wrote:
> >> pauldepst...@att.net 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
  Réponse avec citation
Vieux 06/12/2007, 08h39   #6
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query on design of classes

>> 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;

}
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h59.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24725 seconds with 14 queries