返回类的私有指针成员变量的引用

来源:互联网 发布:成都行知小学小升初 编辑:程序博客网 时间:2024/06/05 04:37

代码

#include<iostream>using namespace std;class student{public:    student(int age)    {        this->m_stdName=new char[10];        strcpy(this->m_stdName,"name");        this->m_stdAge=age;    }    ~student()    {        delete[] this->m_stdName;        this->m_stdAge=0;    }     char* &getStdName()     {         return this->m_stdName;     }protected:private:    char* m_stdName;    int m_stdAge;};void main(){    student std(20);    char* stdname=std.getStdName();    strcpy(stdname,"newName");    delete[]stdname;    cout<<"hello..."<<endl;    system("pause");    return ;}
0 0
原创粉丝点击