E
Eivind Grimsby Haarr
When using polymorphism in a big system, I have sometimes come across
problems when changing the signature of the function in the base class,
and forgetting to change the signature of the derived functions
accordingly. (Most often when only some of the derived classes redefine
it.) Accessing the objects with base class pointers, the version in the
base class is called, which is not what I wanted. This is most often very
difficult to detect.
This has made me come up with an idea for a compiler/language feature that
would fix this problem: A keyword in front of the derived function that
says that this function is derived from a base class, and the compiler
should issue an error if a function with a similar signature doesn't
exist in one of the base classes.
What do you think about this idea? Is it just too much pain for a rare
problem?
Maybe anyone have other ideas on how to prevent such problems? Using
macros to declare the signatures is of course one idea, but true
C++-programmers don't like macros, right? (Perfect design and
programming from the beginning would prevent it, of course, but even
programmers are humans...)
Naming the keyword 'isderived', I'll illustrate my idea with an example:
Function f(double) is performing some calculation. In the base class B, a
default calculation is provided, but a few of the many classes that derive
from B has their own implementation.
-------
class B {
virtual double f(double i) {...} // Uups! Forgot to declare 'const'
}
class OneOfManyDerivedClasses : public B {
isderived double f(double i) {...} // Should be derived from base class
}
vector<B*> v;
....
// Fill the vector with objects of different derived classes
....
int sum = 0;
for (it = v.begin(); it != v.end(); ++it) {
sum+=(*v)->f(number);
}
------
If I later change the base class function to const, the function in the
derived class will not be called when iterating through the vector. If
this calculation is part of a complex calculation with many classes, this
is difficult to detect. With the 'isderived' keyword, a compiler error
would be issued, and the problem prevented.
Any views?
Mvh
Eivind Grimsby Haarr
[ 97 07 66 58 / (e-mail address removed) ]
problems when changing the signature of the function in the base class,
and forgetting to change the signature of the derived functions
accordingly. (Most often when only some of the derived classes redefine
it.) Accessing the objects with base class pointers, the version in the
base class is called, which is not what I wanted. This is most often very
difficult to detect.
This has made me come up with an idea for a compiler/language feature that
would fix this problem: A keyword in front of the derived function that
says that this function is derived from a base class, and the compiler
should issue an error if a function with a similar signature doesn't
exist in one of the base classes.
What do you think about this idea? Is it just too much pain for a rare
problem?
Maybe anyone have other ideas on how to prevent such problems? Using
macros to declare the signatures is of course one idea, but true
C++-programmers don't like macros, right? (Perfect design and
programming from the beginning would prevent it, of course, but even
programmers are humans...)
Naming the keyword 'isderived', I'll illustrate my idea with an example:
Function f(double) is performing some calculation. In the base class B, a
default calculation is provided, but a few of the many classes that derive
from B has their own implementation.
-------
class B {
virtual double f(double i) {...} // Uups! Forgot to declare 'const'
}
class OneOfManyDerivedClasses : public B {
isderived double f(double i) {...} // Should be derived from base class
}
vector<B*> v;
....
// Fill the vector with objects of different derived classes
....
int sum = 0;
for (it = v.begin(); it != v.end(); ++it) {
sum+=(*v)->f(number);
}
------
If I later change the base class function to const, the function in the
derived class will not be called when iterating through the vector. If
this calculation is part of a complex calculation with many classes, this
is difficult to detect. With the 'isderived' keyword, a compiler error
would be issued, and the problem prevented.
Any views?
Mvh
Eivind Grimsby Haarr
[ 97 07 66 58 / (e-mail address removed) ]