开发者社区> 问答> 正文

CString移植到linux下的类型转换问题

CString str;
str = "hello,world";

printf("%sn",str);

这个printf语句,在windows下行正确运行;
但我一直不知道这样调用它到底调用了那一个operator的转换?

我在Linux下写CString类时,定义了
operator char*()
和operator const char*()
两个类型转换,可是
printf("%sn",str);这种写法编译时警告通过,但运行时出现错误.
如果写成printf("%sn",(char*)str);一切都OK了.

想问一下,windows下是如何实现的??

展开
收起
a123456678 2016-06-12 14:04:45 3018 0
1 条回答
写回答
取消 提交回答
  • #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    class MyString
    {
    
    public:
     char *teststr;
     MyString()
     {
     teststr="testit";
     }
    
    };
    int main(int argc, char* argv[])
    {
     MyString mys;
     printf("%s",mys.teststr);
     return 0;
    }
    
    必须要这样才行
    2019-07-17 19:33:56
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载