RezaRob said:
Is there any way to prevent implicit type conversion when parameter
passing.
For example between these types:
class Window * win1;
class BorderedWindow * win2;
Assuming that BorderedWindow derives from Window, there are two possible
meanings to your question:
a) Disable conversion based upon dynamic type checking.
This is something that the function body will have to do. At compile time
the dynamic type is (treated as) unknown.
b) Disable conversion based upon the static parameter type.
There are options:
b1) You can trigger a linker error by overloading with an undefined
function:
class Base {};
class Derived : public Base {};
void function ( Base * b_ptr ) {}
// unimplemented:
void function ( Derived * d_ptr );
int main ( void ) {
Derived * b_ptr = new Derived;
function ( ptr );
}
b2) You can trigger a compiler error using templates:
struct yes_type { char dummy; };
struct no_type { yes_type a; yes_type b; };
template < bool b, typename T >
struct enable_if;
template < typename T >
struct enable_if<true,T> { typedef T type; };
template < bool b, typename T >
struct disable_if;
template < typename T >
struct disable_if<false,T> { typedef T type; };
template < typename S, typename T >
struct same_type {
typedef no_type type;
static const bool value = false;
}; // same_type<>
template < typename S >
struct same_type<S,S> {
typedef yes_type type;
static const bool value = true;
}; // same_type<>
class Base {};
class Derived : public Base {};
template < typename BasePtr >
void function ( BasePtr b_ptr,
typename enable_if< s
ame_type<BasePtr,Base*>::value, void* >::
type dummy = 0 ) {
}
This will, in fact, prevent _all_ conversions to Base* from any derived
type. If you want to disable specific conversions, you can vary the idea
using disable_if instead of enable_if.
Best
Kai-Uwe Bux