打印

[原创] 指针!!慎用(c++)

指针!!慎用(c++)

#include <iostream>


using namespace std;


class IntCell
{
public:
explicit IntCell( int initialValue = 0 )
{ storedValue = new int( initialValue ); }

int read( ) const
{ return *storedValue; }
void write( int x )
{ *storedValue = x; }
private:
int *storedValue;
};


int main()
{
IntCell a( 2 );
IntCell b = a;
IntCell c;

c = b;
a.write( 4 );
cout << a.read( ) << endl << b.read( ) << endl << c.read( ) << endl;
return 0;

}



输出结果:
4
4
4

TOP