S
Simon Elliott
A fairly standard polymorphic class which needs a reference to an
object:
class bar
{
int i1_;
};
class fooBase
{
public:
fooBase(bar& barRef):barRef_(barRef){}
virtual ~fooBase();
private:
bar& barRef_;
int i1_;
};
class fooDerived1ublic fooBase
{
public:
fooDerived1(bar& barRef):fooBase(barRef){}
private:
int i2_;
};
int main(int argc, char* argv[])
{
bar myBar;
fooBase* myFooBase = new fooDerived1(myBar);
delete myFooBase;
return 0;
}
Fine as far as it goes, but if you end up with several dozen or several
hundred fooDerivedN, maintenance becomes a bit tedious.
Let's say you wanted to add a reference to a bengist as well as a bar.
You have to change every single constructor of
fooDerived1..fooDerivedN. And if this is part of a large project with
several developers, you have to check out all these files meaning
no-one else can be working on them. Even if the change only takes 5
minutes this can be very disruptive.
Admittedly the compiler will let you know if you've missed one, but to
me it seems untidy to have to change constructors of lots of derived
classes. Is there a better way?
object:
class bar
{
int i1_;
};
class fooBase
{
public:
fooBase(bar& barRef):barRef_(barRef){}
virtual ~fooBase();
private:
bar& barRef_;
int i1_;
};
class fooDerived1ublic fooBase
{
public:
fooDerived1(bar& barRef):fooBase(barRef){}
private:
int i2_;
};
int main(int argc, char* argv[])
{
bar myBar;
fooBase* myFooBase = new fooDerived1(myBar);
delete myFooBase;
return 0;
}
Fine as far as it goes, but if you end up with several dozen or several
hundred fooDerivedN, maintenance becomes a bit tedious.
Let's say you wanted to add a reference to a bengist as well as a bar.
You have to change every single constructor of
fooDerived1..fooDerivedN. And if this is part of a large project with
several developers, you have to check out all these files meaning
no-one else can be working on them. Even if the change only takes 5
minutes this can be very disruptive.
Admittedly the compiler will let you know if you've missed one, but to
me it seems untidy to have to change constructors of lots of derived
classes. Is there a better way?