开发者

overloading resolution, cpp

I understand that if there are several function with the same name and开发者_如何学C same number of parameters the compiler is trying to find the best match (am I right so far?)

What I don't understand is the difference between type promotion and type conversion.

Say I have this function decleration: void foo (double x) and then inside main:

int x = 5;
foo(x);

Is that considered conversion or promotion?


Type promotion is special case of type conversion.

http://en.wikipedia.org/wiki/Type_conversion#Type_promotion


Your example wont work

you would need to have 2 methods for overloading

1.) void foo(double x){method code} and

2.) void foo(int x){method code}

Then when you run the code

int x = 5;

foo(5)

The compiler or run time environment knows which method to call based on the input type you passed in.

If I want to convert an int into a double that is different. I am not sure what language you are using but in Java you would do the conversion using type casting

this is type casting and will convert a double to an int. You will loose the decimal part if there is one.

double d = 5; int i = (int)d;

I think this is what you are asking. If not please clarify a little

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜