# 温故知新----再谈构造函数 (转)

----再谈构造函数XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

classA a;  //构造函数不需要参数

1：构造函数没有参数

2：构造函数有参数但可以不给出

class A{

public:

A();  //构造函数没有参数

A( int I = 10 );  //构造函数的参数有缺省值，可以不用给出

};

classA a( a1 );  //构造函数有参数，而参数为一个相同的类型

int a = 3;

int b;

b = a;  //这样的话，b中就有和a一样的内容了

int a = 3;

long b;

b = a;  //这样的话，b也能包含有和a一样的内容了

int a = 3;

int b;

//假设这是a(int)的存储空间

0

3

//假设这是b(int)的存储空间

?

?

b =a ;

//将a的内容拷贝到b中

0

3

| |  | |

?

?

//a

0

3

//b

0

3

#include

using namespace std;

class A{

private:

int x;

int y;

int z;

public:

A():x(0),y(0),z(0){ }

A( int _x = 0 , int _y = 0 , int _z = 0 ):x(_x),y(_y),z(_z){ }

friend ostream& operator <<( ostream& , A const& );

};

ostream& operator <<( ostream& out , A const& arg )

{

out << "This is a Instance of A" << endl;

out << "Member Data x is : " << arg.x << endl;

out << "Member Data y is : " << arg.y << endl;

out << "Member Data z is : " << arg.z << endl;

return out;

}

void main()

{

A a( 1 , 12 ,123 );

A b(a);

cout << "This is a!" << endl;

cout << a << endl;

cout << "b is a copy of a!" << endl;

cout << b;

}

This is a!

This is a Instance of A

Member Data x is : 1

Member Data y is : 12

Member Data z is : 123

b is a copy of a!

This is a Instance of A

Member Data x is : 1

Member Data y is : 12

Member Data z is : 123

#include

#include

#include

using namespace std;

class A{

private:

char * data;

public:

A():data(NULL){ }

A( char * _data ):data(NULL)

{

if( !_data )

return;

int length = strlen(_data) +1;

data = new char[length];

memcpy( data , _data , length );

}

~A()

{

if( data )

delete data;

}

void Clear( void )

{

if( data )

{

memset( data , 0 , strlen( data ) );

delete data;

}

data = NULL;

}

friend ostream& operator <<( ostream& , A const& );

};

ostream& operator <<( ostream& out , A const& arg )

{

out << "This is a Instance of A" << endl;

if( arg.data && *arg.data )

out << "Member Data data is : " << arg.data << endl;

else

out << "Member Data data is : NULL" << endl;

return out;

}

void main()

{

A a( "abcdefg" );

A b(a);

cout << "This is a!" << endl;

cout << a << endl;

cout << "b is a copy of a!" << endl;

cout << b << endl;

a.Clear();

cout << "Where a's mem clear!" << endl;

cout << a;

cout << "God! b's mem clear!" << endl;

cout << b << endl;

}

This is a!

This is a Instance of A

Member Data data is : abcdefg

b is a copy of a!

This is a Instance of A

Member Data data is : abcdefg

Where a's mem clear!

This is a Instance of A

Member Data data is : NULL

God! b's mem clear!

This is a Instance of A

Member Data data is : NULL  //不！a中释放了内存连带着b的一起释放掉了。

#include

#include

#include

class A{

private:

char * data;

public:

A():data(NULL){ }

A( char * _data ):data(NULL)

{

if( !_data )

return;

int length = strlen(_data) +1;

data = new char[length];

memcpy( data , _data , length );

}

A( A const& arg )

{

if( !arg.data )

return;

int length = strlen(arg.data) +1;

data = new char[length];

memcpy( data , arg.data , length );

}

~A()

{

if( data )

delete data;

}

void Clear( void )

{

if( data )

{

memset( data , 0 , strlen( data ) );

delete data;

}

data = NULL;

}

friend ostream& operator <<( ostream& , A const& );

};

ostream& operator <<( ostream& out , A const& arg )

{

out << "This is a Instance of A" << endl;

if( arg.data && *arg.data )

out << "Member Data data is : " << arg.data << endl;

else

out << "Member Data data is : NULL" << endl;

return out;

}

void main()

{

A a( "abcdefg" );

A b(a);

cout << "This is a!" << endl;

cout << a << endl;

cout << "b is a copy of a!" << endl;

cout << b << endl;

a.Clear();

cout << "Where a's mem clear!" << endl;

cout << a;

cout << "Good! b's mem not clear!" << endl;

cout << b << endl;

}

This is a!

This is a Instance of A

Member Data data is : abcdefg

b is a copy of a!

This is a Instance of A

Member Data data is : abcdefg

Where a's mem clear!

This is a Instance of A

Member Data data is : NULL

Good! b's mem not clear!

This is a Instance of A

Member Data data is : abcdefg  //哈哈，这正是我想得到的结果。

class A;

class B: public A{

};

B b;

A a(b);

void function( A a);

classA a = a1;  //拷贝构造函数

ClassA a(a1);  //可以改成这种形式

2001/9/13

• 博文量
6241
• 访问量
2447986