I wrote the following piece of code on codepad but I do not understand how/why it should compile.
#include<iostream>
using namespace std;
typedef double dObject;
typedef int iObject;
typedef void (*swapfuncptr)(dObject, dObject);
void swap(dObject a,dObject b) {
cout << a << " " << b << endl;
dObject tmp;
tmp = a;
a = b;
b = tmp;
cout << a << " " << b << endl;
}
int main() {
double a = 7.5, b = 5.3;
swapfuncptr swapptr1;
swapptr1 = &swap;
swapptr1(a, b);
int c = 3, d = 2;
swapfuncptr swapptr2;
swapptr2 = &swap;
swapptr2(c, d);
swapfuncptr swapptr3;
swapptr3 = &swap;
swapptr3('r', 'd');
return 0;
}
So dobject is only for doubles for the functions with integer parameters also work. I do not get how this works.
Can someone please explain.
Thanks S
int
is implicitly convertable to double
. Creating a typedef
for double
does not change that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.