|
-
October 22nd, 2001, 06:46 AM
#1
Automatic casting of in-parameter
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|