为什么不能这样写返回字符串的函数
class student{
char name[20];
int age;
char sex;
public:
char getname()
{
return name;
}
};
#include<iostream> using std::cin; using std::cout; using std::endl; #define NAMELEN 20 class student { char name[NAMELEN]; int age; char sex; public: student(char* szName){strlen(szName)>(NAMELEN-1) ? NULL : strcpy(name,szName);} char* getname() { return name; } }; void main() { student st001("张三"); student* pst002 = new student("王麻子"); cout<<st001.getname()<<endl; cout<<pst002->getname()<<endl; delete pst002; }