Click to See Complete Forum and Search --> : Automatic casting of in-parameter


kjetil_kng
October 22nd, 2001, 06:46 AM
Hi,

I have two classes: class A and class B. class B inherits from A and both have a public function named func(). func() in class A takes an int as parameter, and class B takes a double.

After creating an instance of class B, I wish to call func(int) using the class B object. But how can I do this? When calling B objB.func(int i=40)

the in parameter is automatically casted to a double and func(double) in class B is run.

Can someone explain this "unexpected" behaviour? I have no compiler warnings/errors.

Here is my code:


include <stdio.h>

//! class A with int as parameter to its func()
class A
{
public:
void func(int param){printf("A::func(int)\n");};
};


//! class B with double as parameter to its func()
class B : public A
{
public:
void func(double param){printf("B::func(double)\n");};
};



//! Test app
int main(void)
{
double dNum = 3.14;
int iNum = 1;

A objA;
B objB;

objA.func(iNum);
objB.func(dNum);
objB.func(iNum); //parameter is automatically casted to double!

return 0;
}


// PROGRAM OUTPUT:
//
// A::func(int)
// B::func(double)
// B::func(double)





Kjetil

Igor Soukhov
October 22nd, 2001, 10:16 AM
Hi ! You have to use using declaration to make A::func(int) visible inside class B:

so the code will be looks like:


include <stdio.h>

//! class A with int as parameter to its func()
class A
{
public:
void func(int param){printf("A::func(int)\n");};
};


//! class B with double as parameter to its func()
class B : public A
{
public:
using A::func;//using declaration
void func(double param){printf("B::func(double)\n");};
};



//! Test app
int main(void)
{
double dNum = 3.14;
int iNum = 1;

A objA;
B objB;

objA.func(iNum);
objB.func(dNum);
objB.func(iNum); //parameter is automatically casted to double!

return 0;
}






Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)

Best regards,

-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com

Member of Russian Software Developer Network http://rsdn.ru

Yoav A
October 22nd, 2001, 11:32 AM
You need to declare func() as a virtual function.

kjetil_kng
October 23rd, 2001, 03:51 AM
Thank you for your reply. I've tested it and it works! :-)

I read something about silent type conversion yesterday in one of Scott Meyers' books. I believe this was what that really happened in my example.


Here is an extract:

"C++ allows compilers to perform implicit conversions between types. In
honor of its C heritage, for example, the language allows silent conversions from
char to int and from short to double. This is why you can pass a short to a function
that expects a double and still have the call succeed. The more frightening conversions
in C — those that may lose information — are also present in C++, including conversion
of int to short and double to (of all things) char."

...

"You can't do anything about such conversions, because they're hard-coded into the language."


Excellent book by the way ("Effective C++: 35 ways to improve your C++") .


Regards,

Kjetil

kjetil_kng
October 23rd, 2001, 04:00 AM
Thanks for your reply.

But declaring func() as virtual functions doesn't work. I believe the reason to this is that the compiler performs a silent type conversion from int to double (you'll get similar results changing the func(int) function with a func(char) function).

Please look Look at Igor Soukhov's post for a solution, and my next post for an extract from one of Scott Meyer's books discussing silent type conversion in C++.


Regards,

Kjetil