RE: [Dev-C++] Overloading & reliability
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-05-04 10:04:43
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of rchavez
> Sent: Saturday, May 04, 2002 4:13 AM
> To: dev...@li...
> Subject: [Dev-C++] Overloading & reliability
>
>
> Hi,
>
> The thread on 'Begginer's question, Function overloading'
> made me wonder about
> the following:
>
> Following Brian's example (I'm not criticizing his code),
> reproduced partially
> below, it's clear how overloading works. But it seems to me
> it could be
> a very dangerous feature of the C++ language. If you meant to call
> function with 2 parameters 1.1 and 2.2 but wrote a comma
> instead you wouldn't
> get a error message, you would get a totally different function.
>
> I would imagine the 2 functions would be similar or else they
> would have
> different names right? So it might be very difficult to
> detect that the
> wrong function has been called in a large program.
>
> When a support programmer changes the 2,2 to 2,3 really bad
> things might happen.
>
> Overloading seems very risky in critical applications with
> rare corner cases.
> It's the opposite of the graceful failure I would want in an
> airplane control
> system for example. After all, you never test every possibility.
>
> Comments?
In cases where the same processing of data takes place, templates should
be used. Compiler cannot protect us from implicit conversions in
function overloading. Consider the code:
void something(int x)
{
// ...
}
void something(char c)
{
// ..
}
int main()
{
double x=7.3;
// void something(int) is called, data truncated to 7.
// No compiler warnings or errors.
something(x);
}
Also maintenance can be a nightmare in the above.
The superior alternative which should be used:
template <class T>
void something(const T &x)
{
// ...
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|