赋值操作符的重载情况
#include <iostream>
#include <string>
using namespace std;
class Test
{
int * m_pointer;
public :
Test ( )
{
m_pointer = NULL ;
}
Test ( int i)
{
m_pointer = new int ( i) ;
}
Test ( const Test& obj)
{
m_pointer = new int ( * obj. m_pointer) ;
}
Test& operator = ( const Test& obj)
{
if ( this != & obj)
{
delete m_pointer;
m_pointer = new int ( * obj. m_pointer) ;
}
return * this ;
}
void print ( )
{
cout << "m_pointer = " << hex << m_pointer << endl;
}
~ Test ( )
{
delete m_pointer;
}
} ;
int main ( )
{
Test t1 = 1 ;
Test t2;
t2 = t1;
t2 = t2;
t1. print ( ) ;
t2. print ( ) ;
return 0 ;
}
数组类
#pragma once
#ifndef _INTARRAY_H_
#define _INTARRAY_H_
class IntArray
{
private :
int m_length;
int * m_pointer;
IntArray ( int len) ;
IntArray ( const IntArray& obj) ;
bool construct ( ) ;
public :
static IntArray* NewInstance ( int length) ;
int length ( ) ;
bool get ( int index, int & value) ;
bool set ( int index, int value) ;
int & operator [ ] ( int index) ;
IntArray& operator = ( const IntArray& obj) ;
IntArray& self ( ) ;
~ IntArray ( ) ;
} ;
#endif
#include "IntArray.h"
IntArray:: IntArray ( int len)
{
m_length = len;
}
bool IntArray:: construct ( )
{
bool ret = true ;
m_pointer = new int [ m_length] ;
if ( m_pointer)
{
for ( int i = 0 ; i < m_length; i++ )
{
m_pointer[ i] = 0 ;
}
}
else
{
ret = false ;
}
return ret;
}
IntArray* IntArray:: NewInstance ( int length)
{
IntArray* ret = new IntArray ( length) ;
if ( ! ( ret && ret- > construct ( ) ) )
{
delete ret;
ret = 0 ;
}
return ret;
}
int IntArray:: length ( )
{
return m_length;
}
bool IntArray:: get ( int index, int & value)
{
bool ret = ( 0 <= index) && ( index < length ( ) ) ;
if ( ret)
{
value = m_pointer[ index] ;
}
return ret;
}
bool IntArray:: set ( int index, int value)
{
bool ret = ( 0 <= index) && ( index < length ( ) ) ;
if ( ret)
{
m_pointer[ index] = value;
}
return ret;
}
int & IntArray:: operator [ ] ( int index)
{
return m_pointer[ index] ;
}
IntArray& IntArray:: operator = ( const IntArray& obj)
{
if ( this != & obj)
{
int * pointer = new int [ obj. m_length] ;
if ( pointer)
{
for ( int i = 0 ; i < obj. m_length; i++ )
{
pointer[ i] = obj. m_pointer[ i] ;
}
m_length = obj. m_length;
delete m_pointer;
m_pointer = pointer;
}
}
return * this ;
}
IntArray& IntArray:: self ( )
{
return * this ;
}
IntArray:: ~ IntArray ( )
{
delete [ ] m_pointer;
}
#include <iostream>
#include <string>
#include "IntArray.h"
using namespace std;
int main ( )
{
IntArray* a = IntArray:: NewInstance ( 5 ) ;
IntArray* b = IntArray:: NewInstance ( 10 ) ;
if ( a && b)
{
IntArray& array = a- > self ( ) ;
IntArray& brray = b- > self ( ) ;
cout << "array.length() = " << array. length ( ) << endl;
cout << "brray.length() = " << brray. length ( ) << endl;
array = brray;
cout << "array.length() = " << array. length ( ) << endl;
cout << "brray.length() = " << brray. length ( ) << endl;
}
delete a;
delete b;
return 0 ;
}
一般原则
错误示例
#include <iostream>
#include <string>
using namespace std;
int main ( )
{
const char * z p = "12345" ;
string s = "" ;
s. reserve ( 10 ) ;
for ( int i= 0 ; i< 5 ; i++ )
{
s[ i] = p[ i] ;
}
cout << s << endl;
return 0 ;
}
小结
深拷贝必须重载赋值操作符 string类通过一个数据空间保存字符数据,一个成员变量保存字符串长度 C++尽量避免c语言开发思想