您的位置:程序门 -> c/c++ -> c++ 语言



请问我这个重载有什么问题?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请问我这个重载有什么问题?
发表于:2007-01-23 11:24:26 楼主
class   testoperator
{
public:
testoperator&   operator=   (const   testoperator&);
void   set_len(int   len)   {   m_len   =   len;}
int   get_len()   const   {   return   m_len;}
void   set_width(int   width)   {   m_width   =   width;}
int   get_width()   const   {   return   m_width;}
void   set_name(string   name)   {m_name   =   name;   }
string   get_name()   const   {return   m_name;}
private:
int   m_len;
int   m_width;
string   m_name;

};

testoperator&   testoperator::operator=   (const   testoperator&   roper)
{
testoperator   temp;
temp.m_len   =   roper.m_len;
temp.m_width   =   roper.m_width;
temp.m_name   =   roper.m_name;
return   temp;
}

int   main(void)
{
testoperator   testoper1,testoper2;
testoper2.set_len(1);
testoper2.set_width(2);
testoper2.set_name( "bin ");

testoper1   =   testoper2;//   测试重载=

getchar();
return   1;
}


testoper1   还是得不到testoper2的赋值
发表于:2007-01-23 11:35:261楼 得分:0
怎么能返回局部对象的引用呢

你应该修改*this,返回*this
发表于:2007-01-23 11:35:592楼 得分:0
testoperator&   testoperator::operator=   (const   testoperator&   roper)
{
m_len   =   roper.m_len;
m_width   =   roper.m_width;
m_name   =   roper.m_name;
return   *this;
}
发表于:2007-01-23 11:37:063楼 得分:0
你全赋给了temp,当然得不到值。
                  m_len   =   roper.m_len;
m_width   =   roper.m_width;
m_name   =   roper.m_name;
return   *this;
兄弟,找本c++   primer,认真补课吧。
发表于:2007-01-23 11:39:124楼 得分:0
没有指针和其他特殊要求,用编译器生成的赋值函数就可以了.

testoperator&   testoperator::operator=   (const   testoperator&   roper)
{
        if(this   !=   &roper){
                m_len   =   roper.m_len;
                m_width   =   roper.m_width;
                m_name   =   roper.m_name;
        }
        return   *this;
}
发表于:2007-01-23 12:19:115楼 得分:0
我是lz;
      请问c++   primer里的几个问题,程序:
class   string   {
public:
//   char*   的赋值操作符
string&   operator=(   const   char   *   );
//   ....
private:
int   _size;
char   *_string;
};

string&   string::operator=(   const   char   *sobj   )
{
//   sobj   是个空指针
if   (   !   sobj   )   {
_size   =   0;
delete[]   _string;
_string   =   0;
}
else   {
_size   =   strlen(   sobj   );
delete[]   _string;
_string   =   new   char[   _size   +   1   ];
strcpy(   _string,   sobj   );
}
return   *this;
}

delete[]   _string;   是什么意思?它又没有new,怎么就delete了?
发表于:2007-01-23 12:28:176楼 得分:0
构造函数中new了.赋值的目标是一个已存在的对象.
发表于:2007-01-23 12:58:117楼 得分:0
我是lz;
      请问c++   primer里的几个问题,程序:
class   string   {
public:
//   char*   的赋值操作符
string&   operator=(   const   char   *   );
//   ....
private:
int   _size;
char   *_string;
};

string&   string::operator=(   const   char   *sobj   )
{
//   sobj   是个空指针
if   (   !   sobj   )   {
_size   =   0;
delete[]   _string;
_string   =   0;
}
else   {
_size   =   strlen(   sobj   );
delete[]   _string;
_string   =   new   char[   _size   +   1   ];
strcpy(   _string,   sobj   );
}
return   *this;
}

delete[]   _string;   是什么意思?它又没有new,怎么就delete了?


快速检索

最新资讯
热门点击