C
Christof Warlich
Hi,
please consider the following code, using named parameters for object
instantiation:
class Base {
public:
Base(): _varBase(0) {}
Base &varBase(int var) {_varBase = var; return *this;}
private:
int _varBase;
};
class Derived: public Base {
public:
Derived(): _varDerived(0) {}
Derived &varDerived(int var) {_varDerived = var; return *this;}
// ugly forwarding function
Derived &varBase(int var) {varBase(var); return *this;}
private:
int _varDerived;
};
int main() {
Derived w = Derived().varDerived(42);
// the forwarding function is needed for all of these.
Derived x = Derived().varBase(4711);
Derived y = Derived().varDerived(42).varBase(4711);
Derived z = Derived().varBase(4711).varDerived(42);
}
It works fine so far, but now imagine that the classes have more
member variables and that the inheritance hierarchy is deeper than 2
(i.e. we have classes DerivedFromDerived, ...). Then, _each_ of the
derived classes would need a forwarding function for _every_ variable
in _any_ of its base classes, which doesn't scale very well.
Is anyone aware of a way to avoid these forwarding function(s)?
Thanks for any ideas,
Chris
please consider the following code, using named parameters for object
instantiation:
class Base {
public:
Base(): _varBase(0) {}
Base &varBase(int var) {_varBase = var; return *this;}
private:
int _varBase;
};
class Derived: public Base {
public:
Derived(): _varDerived(0) {}
Derived &varDerived(int var) {_varDerived = var; return *this;}
// ugly forwarding function
Derived &varBase(int var) {varBase(var); return *this;}
private:
int _varDerived;
};
int main() {
Derived w = Derived().varDerived(42);
// the forwarding function is needed for all of these.
Derived x = Derived().varBase(4711);
Derived y = Derived().varDerived(42).varBase(4711);
Derived z = Derived().varBase(4711).varDerived(42);
}
It works fine so far, but now imagine that the classes have more
member variables and that the inheritance hierarchy is deeper than 2
(i.e. we have classes DerivedFromDerived, ...). Then, _each_ of the
derived classes would need a forwarding function for _every_ variable
in _any_ of its base classes, which doesn't scale very well.
Is anyone aware of a way to avoid these forwarding function(s)?
Thanks for any ideas,
Chris