注册 登录
编程论坛 C++教室

该怎么改才显示返回的名字?

sam031 发布于 2013-03-01 22:30, 280 次点击
程序代码:
#include <iostream>
#include <string>
using namespace std;
string* getname();
int main()
{
    string* name;
    name=getname();
    cout<<name<<" at "<<(int*)name<<endl;
    delete name;
    return 0;
}
string* getname()
{
    string temp;
    cout<<"Enter your name:";
    getline(cin,temp);
    string* pn=new string;
    *pn=temp;
    return pn;
}
这个,输入名字后返回的是地址。。。。
该怎么改才显示返回的名字?
另外 delete name可以了吗?不需要再delete pn吗?不会泄漏吗?不是太明白,请指教!
谢谢
2 回复
#2
zhoufeng19882013-03-01 23:21
cout<<*name<<" at "<<name<<endl

你的
cout << name ...
,name是一个指向string类型的指针,直接
cout << name...
当然输出的是地址了。
delete name就可以了。
#3
sam0312013-03-02 09:41
回复 2楼 zhoufeng1988
哦,对哦~抓破头在想是不是*pn指针有问题,原来是这个,懂了,谢谢楼上的解答!
1