标题:关于引用T-T
只看楼主
bid2938692
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2009-9-23
结帖率:76.92%
 问题点数:0 回复次数:0 
关于引用T-T
#include <iostream>
using namespace std;
#include <cstring>
class Namelist
{    char *name;
    public:
        Namelist(char *p)
        {    name=new char[strlen(p)+1];
            if(name!=0)strcpy(name,p);
        }
        ~Namelist()
        {    delete[] name;
        }
        Namelist& operator=(char *p);
        Namelist& operator=(Namelist&);
        void display(){cout<<name<<endl;}
};
Namelist& Namelist::operator=(char *p)
{    name=new char[strlen(p)+1];
    if(name!=0)strcpy(name,p);
    return *this;
}
Namelist& Namelist::operator=(Namelist& a)
{    if(this!=&a)
    {    delete name;
        name=new char[strlen(a.name)+1];
        if(name!=0)strcpy(name,a.name);
    }
    return *this;
}
int main()
{    Namelist n1("rO"),n2("lo");
    cout<<"赋值前的数据:"<<endl;
    n1.display();
    n2.display();
    cout<<"赋值后的数据:"<<endl;
    n2=n1;
    n1.display();
    n2.display();
    return 0;
}
为什么不能把Namelist& Namelist::operator=(Namelist& a)中的两个&去掉呢,如果去掉一个会怎么样呢,可以说明下吗??
搜索更多相关主题的帖子: name void display include public 
2010-04-24 19:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-304310-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.115688 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved