作业题 关于new分配动态空间的

yao90928 2012-05-21 01:32:09
题目很简单,我贴有类型转化函数那有问题部分,把m_name声明为char*类型的时候 程序能通过
但是声明为string类型时候就不能

#include <iostream>
#include <string>
using namespace std;

class student
{
public:
student(int x1, char* x2):m_num(x1),m_name(x2){}
operator char* ()const
{
return m_name;
}
operator int ()
{
return m_num;
}

private:
int m_num;
char* m_name;

};
int main()
{ int num;
char* name;

student *p=new student(11,"uh");

num=(int)p;
name=(char* )p;

cout<<num<<endl;
cout<<name<<endl;
return 0;
}
是能通过的

#include <iostream>
#include <string>
using namespace std;

class student
{
public:
student(int x1, string x2):m_num(x1),m_name(x2){}
operator string ()const
{
return m_name;
}
operator int ()
{
return m_num;
}

private:
int m_num;
string m_name;

};
int main()
{ int num;
string name;

student *p=new student(11,"uh");

num=(int)p;
name=(string )p;

cout<<num<<endl;
cout<<name<<endl;
return 0;
}
这个就有问题了
提示类型转换”: 无法从“student *”转换为“std::string”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
这个是什么原因..
不是计算机专业的...老师搞硬件的..c++他也只懂一点,他也说不清。特来请教
...全文
153 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangchen456 2012-05-21
  • 打赏
  • 举报
回复
num=(int)(*p);
name=(string )(*p);

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧