异常安全的赋值运算符重载函数 C++ String实现

本文详细介绍了如何实现一个改进的CMyString类,包括赋值运算符重载、异常安全性和优雅的内存管理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常安全的赋值运算符重载函数

题目:类CMyString的声明如下:

复制代码
class CMyString
{
public:
CMyString(
char* pData = NULL);
CMyString(
const CMyString& str);
~CMyString(void);
CMyString
& operator = (const CMyString& str);

private:
char* m_pData;
};
复制代码

  请实现其赋值运算符的重载函数,要求异常安全,即当对一个对象进行赋值时发生异常,对象的状态不能改变。

分析:首先我们来看一般C++教科书上给出的赋值运算符的重载函数:

复制代码
CMyString& CMyString::operator =(const CMyString &str)
{
if(this == &str)
return *this;

delete []m_pData;
m_pData
= NULL;

m_pData
= new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);

return *this;
}
复制代码

  我们知道,在分配内存时有可能发生异常。当执行语句

new char[strlen(str.m_pData) + 1]发生异常时,程序将从该赋值运算符的重载函数退出不再执行。注意到这个时候语句delete []m_pData已经执行了。也就是说赋值操作没有完成,但原来对象的状态已经改变。也就是说不满足题目的异常安全的要求。

为了满足异常安全这个要求,一个简单的办法是掉换newdelete的顺序。先把内存new出来用一个临时指针保存起来,只有这个语句正常执行完成之后再执行delete。这样就能够保证异常安全了。

下面给出的是一个更加优雅的实现方案:

复制代码
CMyString& CMyString::operator =(const CMyString &str)
{
if(this != &str)
{
CMyString strTemp(str);

char* pTemp = strTemp.m_pData;
strTemp.m_pData
= m_pData;
m_pData
= pTemp;
}

return *this;
}
复制代码

  

  该方案通过调用构造拷贝函数创建一个临时对象来分配内存。此时即使发生异常,对原来对象的状态没有影响。交换临时对象和需要赋值的对象的字符串指针之后,由于临时对象的生命周期结束,自动调用其析构函数释放需赋值对象的原来的字符串空间。整个函数不需要显式用到newdelete,内存的分配和释放都自动完成,因此代码显得比较优雅。

转自于:

http://www.cppblog.com/bellgrade/archive/2009/09/26/97321.html



实现stl string类——转自C++ how to program。

//  String class definition with operator overloading.
#ifndef STRING_H
#define  STRING_H

#include 
< iostream >
using  std::ostream;
using  std::istream;

class  String
{
   friend ostream 
&operator<<( ostream &const String & );
   friend istream 
&operator>>( istream &, String & );
public:
   String( 
const char * = "" ); // conversion/default constructor
   String( const String & ); // copy constructor
   ~String(); // destructor

   
const String &operator=const String & ); // assignment operator
   const String &operator+=const String & ); // concatenation operator

   
bool operator!() const// is String empty?
   bool operator==const String & ) const// test s1 == s2
   bool operator<const String & ) const// test s1 < s2

   
// test s1 != s2
   bool operator!=const String &right ) const
   

      
return !*this == right ); 
   }
 // end function operator!=

   
// test s1 > s2
   bool operator>const String &right ) const
   

      
return right < *this
   }
 // end function operator>
 
   
// test s1 <= s2
   bool operator<=const String &right ) const
   

      
return !( right < *this ); 
   }
 // end function operator <=

   
// test s1 >= s2
   bool operator>=const String &right ) const
   

      
return !*this < right ); 
   }
 // end function operator>=

   
char &operator[]( int ); // subscript operator (modifiable lvalue)
   char operator[]( int ) const// subscript operator (rvalue)
   String operator()( intint = 0 ) const// return a substring
   int getLength() const// return string length
private:
   
int length; // string length (not counting null terminator)
   char *sPtr; // pointer to start of pointer-based string

   
void setString( const char * ); // utility function
}
//  end class String

#endif
源文件
//  String class member-function and friend-function definitions.
#include  < iostream >
using  std::cerr;
using  std::cout;
using  std::endl;

#include 
< iomanip >
using  std::setw;

#include 
< cstring >   //  strcpy and strcat prototypes
using  std::strcmp;
using  std::strcpy;
using  std::strcat;

#include 
< cstdlib >   //  exit prototype
using  std::exit;

#include 
" String.h "   //  String class definition

//  conversion (and default) constructor converts char * to String
String::String(  const   char   * s ) 
   : length( ( s 
!=   0  )  ?  strlen( s ) :  0  )
{
   cout 
<< "Conversion (and default) constructor: " << s << endl;
   setString( s ); 
// call utility function
}
  //  end String conversion constructor

//  copy constructor
String::String(  const  String  & copy ) 
   : length( copy.length )
{
   cout 
<< "Copy constructor: " << copy.sPtr << endl;
   setString( copy.sPtr ); 
// call utility function
}
  //  end String copy constructor

//  Destructor
String:: ~ String()
{
   cout 
<< "Destructor: " << sPtr << endl;
   delete [] sPtr; 
// release pointer-based string memory
}
  //  end ~String destructor

//  overloaded = operator; avoids self assignment
const  String  & String:: operator = const  String  & right )
{
   cout 
<< "operator= called" << endl;

   
if ( &right != this ) // avoid self assignment
   {         
      delete [] sPtr; 
// prevents memory leak
      length = right.length; // new String length
      setString( right.sPtr ); // call utility function
   }
 // end if
   else
      cout 
<< "Attempted assignment of a String to itself" << endl;

   
return *this// enables cascaded assignments
}
  //  end function operator=

//  concatenate right operand to this object and store in this object
const  String  & String:: operator += const  String  & right )
{
   size_t newLength 
= length + right.length; // new length
   char *tempPtr = new char[ newLength + 1 ]; // create memory

   strcpy( tempPtr, sPtr ); 
// copy sPtr
   strcpy( tempPtr + length, right.sPtr ); // copy right.sPtr

   delete [] sPtr; 
// reclaim old space
   sPtr = tempPtr; // assign new array to sPtr
   length = newLength; // assign new length to length
   return *this// enables cascaded calls
}
  //  end function operator+=

//  is this String empty?
bool  String:: operator ! ()  const

   
return length == 0
}
  //  end function operator! 

//  Is this String equal to right String?
bool  String:: operator == const  String  & right )  const

   
return strcmp( sPtr, right.sPtr ) == 0
}
  //  end function operator==

//  Is this String less than right String?
bool  String:: operator < const  String  & right )  const

   
return strcmp( sPtr, right.sPtr ) < 0
}
  //  end function operator<

//  return reference to character in String as a modifiable lvalue
char   & String:: operator [](  int  subscript )
{
   
// test for subscript out of range
   if ( subscript < 0 || subscript >= length )
   
{
      cerr 
<< "Error: Subscript " << subscript 
         
<< " out of range" << endl;
      exit( 
1 ); // terminate program
   }
 // end if

   
return sPtr[ subscript ]; // non-const return; modifiable lvalue
}
  //  end function operator[]

//  return reference to character in String as rvalue
char  String:: operator [](  int  subscript )  const
{
   
// test for subscript out of range
   if ( subscript < 0 || subscript >= length )
   
{
      cerr 
<< "Error: Subscript " << subscript 
           
<< " out of range" << endl;
      exit( 
1 ); // terminate program
   }
 // end if

   
return sPtr[ subscript ]; // returns copy of this element
}
  //  end function operator[]

//  return a substring beginning at index and of length subLength
String String:: operator ()(  int  index,  int  subLength )  const
{
   
// if index is out of range or substring length < 0, 
   
// return an empty String object
   if ( index < 0 || index >= length || subLength < 0 )  
      
return ""// converted to a String object automatically

   
// determine length of substring
   int len;

   
if ( ( subLength == 0 ) || ( index + subLength > length ) )
      len 
= length - index;
   
else
      len 
= subLength;

   
// allocate temporary array for substring and 
   
// terminating null character
   char *tempPtr = new char[ len + 1 ];

   
// copy substring into char array and terminate string
   strncpy( tempPtr, &sPtr[ index ], len );
   tempPtr[ len ] 
= '\0';

   
// create temporary String object containing the substring
   String tempString( tempPtr );
   delete [] tempPtr; 
// delete temporary array
   return tempString; // return copy of the temporary String
}
  //  end function operator()

//  return string length
int  String::getLength()  const  

   
return length; 
}
  //  end function getLength

//  utility function called by constructors and operator=
void  String::setString(  const   char   * string2 )
{
   sPtr 
= new char[ length + 1 ]; // allocate memory

   
if ( string2 != 0 ) // if string2 is not null pointer, copy contents
      strcpy( sPtr, string2 ); // copy literal to object
   else // if string2 is a null pointer, make this an empty string
      sPtr[ 0 ] = '\0'// empty string
}
  //  end function setString 

//  overloaded output operator
ostream  & operator << ( ostream  & output,  const  String  & s )
{
   output 
<< s.sPtr;
   
return output; // enables cascading
}
  //  end function operator<<

//  overloaded input operator
istream  & operator >> ( istream  & input, String  & s )
{
   
char temp[ 100 ]; // buffer to store input
   input >> setw( 100 ) >> temp;
   s 
= temp; // use String class assignment operator
   return input; // enables cascading
}
  //  end function operator>>
***********************************测试的代码****************************************************
//  String class test program.
#include  < iostream >
using  std::cout;
using  std::endl;
using  std::boolalpha;

#include 
" String.h "

int  main()
{
   String s1( 
"happy" );
   String s2( 
" birthday" );
   String s3;

   
// test overloaded equality and relational operators
   cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2
      << "\"; s3 is \"" << s3 << '\"' 
      
<< boolalpha << "\n\nThe results of comparing s2 and s1:"
      
<< "\ns2 == s1 yields " << ( s2 == s1 )
      
<< "\ns2 != s1 yields " << ( s2 != s1 )
      
<< "\ns2 >  s1 yields " << ( s2 > s1 )
      
<< "\ns2 <  s1 yields " << ( s2 < s1 )
      
<< "\ns2 >= s1 yields " << ( s2 >= s1 )
      
<< "\ns2 <= s1 yields " << ( s2 <= s1 );
      

   
// test overloaded String empty (!) operator
   cout << "\n\nTesting !s3:" << endl;

   
if ( !s3 )
   
{
      cout 
<< "s3 is empty; assigning s1 to s3;" << endl;
      s3 
= s1; // test overloaded assignment
      cout << "s3 is \"" << s3 << "\"";
   }
 // end if

   
// test overloaded String concatenation operator
   cout << "\n\ns1 += s2 yields s1 = ";
   s1 
+= s2; // test overloaded concatenation
   cout << s1;

   
// test conversion constructor
   cout << "\n\ns1 += \" to you\" yields" << endl;
   s1 
+= " to you"// test conversion constructor
   cout << "s1 = " << s1 << "\n\n";

   
// test overloaded function call operator () for substring
   cout << "The substring of s1 starting at\n"
      
<< "location 0 for 14 characters, s1(0, 14), is:\n"
      
<< s1( 014 ) << "\n\n";

   
// test substring "to-end-of-String" option
   cout << "The substring of s1 starting at\n"
      
<< "location 15, s1(15), is: "
      
<< s1( 15 ) << "\n\n"

   
// test copy constructor
   String *s4Ptr = new String( s1 );  
   cout 
<< "\n*s4Ptr = " << *s4Ptr << "\n\n";

   
// test assignment (=) operator with self-assignment
   cout << "assigning *s4Ptr to *s4Ptr" << endl;
   
*s4Ptr = *s4Ptr; // test overloaded assignment
   cout << "*s4Ptr = " << *s4Ptr << endl;

   
// test destructor
   delete s4Ptr;     

   
// test using subscript operator to create a modifiable lvalue
   s1[ 0 ] = 'H';      
   s1[ 
6 ] = 'B';
   cout 
<< "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "
      
<< s1 << "\n\n";

   
// test subscript out of range
   cout << "Attempt to assign 'd' to s1[30] yields:" << endl;
   s1[ 
30 ] = 'd'// ERROR: subscript out of range
   return 0;
}
  //  end main
以下是一个自己写的可以使用的版本:

#include <iostream>
#include <map>
#include <algorithm>
#include <limits.h>
#include <assert.h>
#include <stack>
#include <string>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <time.h>
#include <stdio.h>
using namespace std;

class CMyString
{
public:
  CMyString(char* pData = NULL);
  CMyString(const CMyString& str);
  virtual ~CMyString(void);
  CMyString& operator = (const CMyString& str);
  bool operator == (const CMyString& str) const;
  CMyString& operator += (const CMyString& str);
private:
  char* mData;
};
CMyString::CMyString(char* pData) {
  if (pData == NULL) {
    mData = new char[1];
    mData[0] = '\0';
  }
  else {
    int len = strlen(pData);
    mData = new char[len + 1];
    strcpy(mData,pData);
  }
}
CMyString::CMyString(const CMyString& str) {
  int len = strlen(str.mData);
  mData = new char[len + 1];
  strcpy(mData, str.mData);
}
CMyString::~CMyString() {
  delete [] mData;
}
CMyString& CMyString::operator =(const CMyString &str)
{
  if(this != &str)
  {
    CMyString strTemp(str);
    char* pTemp = strTemp.mData;
    strTemp.mData = mData;
    mData = pTemp;
  }
  return *this;
}
bool CMyString::operator == (const CMyString& str) const{
  return strcmp(mData,str.mData) == 0;
}
CMyString& CMyString::operator+=(const CMyString& str) {
  int len1= strlen(mData), len2 = strlen(str.mData), len = len1 + len2; 
  char* tmp = new char[len + 1];
  strcpy(tmp, mData);
  strcpy(tmp+len1, str.mData);
  delete []mData;
  mData = tmp;
  return *this;
}



int main() {
  CMyString str1 = "abcd";
  CMyString str2 = "abcd";
  CMyString str3 = "abcd";
  if (str1 == str2)
    str3 += str2;
  str2 = str3;
  return 0;
}





C++中,**赋值运算符重载(`operator=`)**用于自定义类对象的赋值行为。它必须返回当前对象的引用(`*this`),以支持连续赋值(如`a = b = c`),并正确处理自赋值(如`a = a`)以避免资源泄漏或重复释放。以下是详细说明和示例: --- ### **1. 赋值运算符重载的基本语法** ```cpp class MyClass { public: // 赋值运算符重载 MyClass& operator=(const MyClass& other) { if (this != &other) { // 处理自赋值 // 1. 释放当前对象的资源(如有) // 2. 深拷贝other的资源到当前对象 } return *this; // 返回当前对象的引用 } // 移动赋值运算符C++11起) MyClass& operator=(MyClass&& other) noexcept { if (this != &other) { // 1. 释放当前对象的资源 // 2. 移动other的资源到当前对象 // 3. 将other置于有效但未定义的状态 } return *this; } }; ``` --- ### **2. 关键注意事项** #### **(1)返回当前对象的引用** - 必须返回`*this`(即当前对象的引用),以支持链式赋值: ```cpp MyClass a, b, c; a = b = c; // 等价于 a.operator=(b.operator=(c)) ``` #### **(2)处理自赋值** - 自赋值(如`a = a`)可能导致资源重复释放或未定义行为,需显式检查: ```cpp if (this != &other) { ... } ``` #### **(3)深拷贝与资源管理** - 如果类管理动态资源(如指针、文件句柄等),需在赋值时深拷贝资源,避免浅拷贝导致的双重释放: ```cpp class String { char* data; size_t size; public: String& operator=(const String& other) { if (this != &other) { delete[] data; // 释放旧资源 size = other.size; data = new char[size]; // 分配新资源 std::copy(other.data, other.data + size, data); // 深拷贝 } return *this; } }; ``` #### **(4)移动赋值运算符C++11)** - 移动赋值通过“窃取”右值对象的资源来避免不必要的拷贝,提升性能: ```cpp String& operator=(String&& other) noexcept { if (this != &other) { delete[] data; // 释放当前资源 data = other.data; // 接管右值资源 size = other.size; other.data = nullptr; // 置空右值,避免重复释放 } return *this; } ``` --- ### **3. 完整示例** #### **(1)动态数组类赋值运算符** ```cpp #include <iostream> #include <algorithm> class IntArray { int* data; size_t size; public: // 构造函数 IntArray(size_t n) : size(n), data(new int[n]) {} // 析构函数 ~IntArray() { delete[] data; } // 拷贝赋值运算符 IntArray& operator=(const IntArray& other) { if (this != &other) { delete[] data; // 释放旧资源 size = other.size; data = new int[size]; // 分配新资源 std::copy(other.data, other.data + size, data); // 深拷贝 } return *this; } // 移动赋值运算符 IntArray& operator=(IntArray&& other) noexcept { if (this != &other) { delete[] data; // 释放当前资源 data = other.data; // 接管资源 size = other.size; other.data = nullptr; // 置空右值 other.size = 0; } return *this; } // 打印数组 void print() const { for (size_t i = 0; i < size; ++i) std::cout << data[i] << " "; std::cout << "\n"; } }; int main() { IntArray a(3); a = {1, 2, 3}; // 初始化(假设有初始化列表构造函数) IntArray b(3); b = a; // 调用拷贝赋值 IntArray c = std::move(a); // 调用移动赋值 b.print(); // 输出: 1 2 3 return 0; } ``` #### **(2)拷贝并交换(Copy-and-Swap)惯用法** - 通过`std::swap`简化赋值运算符实现,同时处理自赋值和异常安全: ```cpp #include <utility> // for std::swap class String { char* data; size_t size; public: // 拷贝赋值运算符(使用拷贝并交换) String& operator=(String other) { // 注意:传值以利用拷贝构造函数 std::swap(data, other.data); std::swap(size, other.size); return *this; } // 移动赋值运算符(可省略,由拷贝赋值+传值优化实现) }; ``` --- ### **4. 特殊场景** #### **(1)继承中的赋值运算符** - 派生类的赋值运算符需显式调用基类的赋值运算符: ```cpp class Base { public: Base& operator=(const Base& other) { ... } }; class Derived : public Base { public: Derived& operator=(const Derived& other) { Base::operator=(other); // 调用基类赋值 // 处理派生类成员 return *this; } }; ``` #### **(2)禁止赋值** - 若需禁止赋值操作,可将`operator=`声明为`delete`: ```cpp class NonAssignable { public: NonAssignable& operator=(const NonAssignable&) = delete; }; ``` --- ### **5. 总结** | **要点** | **说明** | |------------------------|--------------------------------------------------------------------------| | **返回值** | 必须返回`*this`的引用(支持链式赋值) | | **自赋值处理** | 显式检查`this != &other`,避免资源重复释放 | | **深拷贝** | 管理动态资源时需深拷贝,避免浅拷贝问题 | | **移动语义(C++11)** | 通过移动赋值提升性能,接管右值资源 | | **异常安全** | 使用拷贝并交换(Copy-and-Swap)惯用法简化实现并保证异常安全 | **核心原则**: 赋值运算符需确保对象在赋值后与源对象逻辑等价,同时正确管理资源(避免泄漏或重复释放),并支持自赋值和链式赋值。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值