📄 subject_65998.htm
字号:
<p>
序号:65998 发表者:ljl 发表日期:2003-12-21 19:55:04
<br>主题:c++ primer的例子,析构出错,找不到原因了,请教!
<br>内容://IntArray.h<BR><BR>#include <iostream><BR>#include <string><BR>#include<cassert><BR>using namespace std;<BR><BR>#ifndef INTARRAY_H<BR>#define INTARRAY_H<BR><BR>class IntArray<BR>{<BR>public:<BR> //构造函数<BR> explicit IntArray(int sz = DefaultArraySize );<BR> IntArray( int* array, int array_size ); <BR> IntArray( const IntArray &rhs );<BR> <BR> //虚拟析构函数<BR> virtual ~IntArray() <BR> { <BR> cout << *ia << endl;<BR> delete[] ia;<BR> }<BR><BR> //equality and inequality operations<BR> bool operator==( const IntArray& ) const;<BR> bool operator!=( const IntArray& ) const;<BR> IntArray& operator=( const IntArray& );<BR> int size() const { return _size; }<BR><BR> //去掉了索引检查功能...<BR> virtual int& operator[]( int index ) { return ia[index];}<BR> virtual void sort(){}<BR> virtual int min() const { return 0; } <BR> virtual int max() const { return 0; } <BR> virtual int find( int value ) const{return 0;}<BR><BR>protected:<BR> //参见13.5节的说明<BR> static int const DefaultArraySize;<BR> void init( int sz, int * array );<BR> int _size;<BR> int *ia;<BR>};<BR><BR>int const DefaultArraySize = 0; //vc中必须在类外部初始化;dev-c中则不必<BR><BR>#endif<BR><BR>/////////////////////////////////////////////////////<BR>//IntArray.cpp<BR>#include <iostream><BR>#include <cassert><BR>using namespace std;<BR><BR>#include "IntArray.h"<BR><BR>void swap( IntArray &ia, int i, int j )<BR>{<BR> int tmp = ia[ i ];<BR> ia[ i ] = ia[ j ];<BR> ia[ j ] = tmp;<BR>}<BR><BR>IntArray::IntArray( int sz )<BR>{ <BR> init( sz, 0 );<BR>}<BR><BR>IntArray::IntArray( int *array, int sz )<BR>{<BR> init( sz, array );<BR>}<BR><BR>IntArray::IntArray( const IntArray &rhs )<BR>{<BR> init( rhs._size, rhs.ia );<BR>}<BR><BR>//page30<BR>void IntArray::init( int sz, int *array )<BR>{<BR> _size = sz;<BR> ia = new int[ _size ];<BR><BR> for(int ix = 0; ix < _size ; ++ix )<BR> {<BR> if( !array )<BR> ia[ ix ] = 0;<BR> else<BR> ia[ ix ] = array[ ix ];<BR> }<BR>}<BR><BR>///////////////////////////////////////////////////////////////<BR><BR>//IntArrayMain.cpp<BR>#include <iostream><BR>#include "IntArray.h"<BR>#include "IntArrayRC.h"<BR><BR>using namespace std;<BR>extern void swap(IntArray&, int ,int );<BR>int main()<BR>{<BR> int array[4] = { 2, 1, 2, 3 };<BR> IntArray ia1(array, 4);<BR> IntArray ia2(array, 4);<BR><BR> cout << "swap() with IntArray ial\n";<BR> swap( ia1, 1, ia1.size() );<BR><BR> cout << "swap() with IntArrayRC ia2\n";<BR> swap( ia2, 1, ia2.size() );<BR><BR> system("pause");<BR> return 0;<BR>}<BR><BR>////////////////////////////////////////////////////////////////<BR>//IntArrayRC.cpp<BR>#include "IntArrayRC.h"<BR>#include <cassert><BR>using namespace std;<BR><BR>inline int& IntArrayRC::operator []( int index )<BR>{<BR> check_range( index );<BR> return ia[ index ];<BR>}<BR><BR>inline void IntArrayRC::check_range ( int index )<BR>{<BR> assert( index >= 0 && index < _size );<BR>}<BR><BR>inline IntArrayRC::IntArrayRC( int sz )<BR>:IntArray( sz ) {}<BR><BR>//书上第一个参数是const int* iar,运行出错,如下可以运行,但析构的时候出错<BR>//inline IntArrayRC::IntArrayRC( const int* iar, int sz ) <BR>inline IntArrayRC::IntArrayRC( int* iar, int sz ) <BR>: IntArray( iar, sz ) {}<BR><BR>//////////////////////////////////////////////////////////////////<BR>//IntArrayRC.h<BR>#include "IntArray.h"<BR><BR>#ifndef IntArrayRC_H<BR>#define IntArrayRC_H<BR><BR>class IntArrayRC : public IntArray<BR>{<BR>public:<BR> IntArrayRC( int sz = DefaultArraySize );<BR> IntArrayRC( int* array, int array_size ); <BR> IntArrayRC( const int* iar, int sz );<BR> IntArrayRC( const IntArrayRC &rhs );<BR> virtual int& operator[]( int );<BR>private:<BR> void check_range( int );<BR>};<BR><BR>#endif<BR><BR>2003-12-21 21:16:46
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -