原文链接:
https://blog.csdn.net/oyoung_2012/article/details/54315963
很多时候,会遇到第三方接口中本来需要使用const char *类型参数的时候,却使用了char *类型,以至于我们原本可以使用如someFunction(“some”), 却编译报错(如某康的SDK)
###针对以上的这种问题,我们可以在对第三方SDK进行本地封装的时候对参数类型进行修改,比如:
1 2 3 4 5
|
void Login(char *ip, unsigned short port, char *userName, char *password);
Login("127.0.0.1", 8000, "admin", "admin");
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class SDK { public: static void login(const char *ip, unsigned short port, const char *userName, const char *password); private: static char *transform(const char *pChar); }
void SDK::login(const char *ip, unsigned short port, const char *userName, const char *password) { char *newIp = transform(ip); char *newUserName = transform(userName); char *newPassword = transform(password); Login(newIp, port, newUserName, newPassword); }
char * SDK::transform(const char *pChar) { return const_cast<char *>(pChar); union { char *p; const char *q;} t; t.q = pChar; return t.p; int address = (int)pChar; return (char *)address; }
|