构造函数

一般情况下,当我们没有为类定义自己的构造函数,拷贝构造函数、拷贝赋值操作符和析构函数时,编译器可以自动生成默认的构造函数、拷贝构造函数、拷贝赋值操作符和析构函数。

例如,对一个空类

class A
{
}

我们可直接使用语句

A a1;  //生成A类型的对象a1,使用默认构造函数

A a2(a1);  //生成A类型的对象a2,使用默认拷贝构造函数

A a3;

a3=a1;  //生成A类型的对象a3,使用默认拷贝赋值操作符

#include <iostream>
using namespace std; 

class A
{
public:
    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
        m_nMember1
=nValue;
    }
private:
    
int m_nMember1;
}; 

int main()
{
    A a1;
    a1.SetMember1(
1);
    cout
<<a1.GetMember1()<<endl; 

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl; 

    a1.SetMember1(
3);
    A a3
=a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}

 Output::

1
2
3
Press any key to continue 
. . .

 但是有几种情况下,编译器不会生成默认的构造函数和拷贝赋值操作符:

1.当类中含有引用成员变量时:

例如,当我们把上面那份代码中的class A的成员变量int m_nMember1;改成int& m_nMember1;时,编译将出错:

error C2512: 'A' : no appropriate default constructor available

当成员变量是引用时,该成员变量必须在构造函数的初始化列表中被初始化。

把以上代码改为:

#include <iostream>
using namespace std;

class A
{
public:
    A(
int& nValue):m_nMember1(nValue){};
    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
        m_nMember1
=nValue;
    }
private:
    
int& m_nMember1;
};
int main()
{
    
int nValue=1;
    A a1(nValue);

    
//a1.SetMember1(1);
    cout<<a1.GetMember1()<<endl;

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl;

    a1.SetMember1(
3);
    A a3
=a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}

 Output:

1
2
3
Press any key to continue 
. . .

同样,当成员变量是引用的时候,编译器不会自动重载=运算符(拷贝赋值操作符)。

#include <iostream>
using namespace std; 

class A
{
public:
    A(
int& nValue):m_nMember1(nValue)
    {
    }

    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
        m_nMember1
=nValue;
    }
private:
    
int& m_nMember1;
}; 

int main()
{
    
int nValue=1;
    A a1(nValue);
    a1.SetMember1(
1);
    cout
<<a1.GetMember1()<<endl; 

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl; 

    a1.SetMember1(
3);
    
int nValue1=4;
    A a3(nValue1);
    a3
a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}

编译会出错:

error C2582: 'operator =' function is unavailable in 'A'

修改代码:

#include <iostream>
using namespace std; 

class A
{
public:
    A(
int& nValue):m_nMember1(nValue)
    {
    }

    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
        m_nMember1
=nValue;
    }

    A
& operator=(A& rhs)
    {
        m_nMember1
=rhs.GetMember1();
        ;
    }
private:
    
int& m_nMember1;
}; 

int main()
{
    
int nValue=1;
    A a1(nValue);
    a1.SetMember1(
1);
    cout
<<a1.GetMember1()<<endl; 

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl; 

    a1.SetMember1(
3);
    
int nValue1=4;
    A a3(nValue1);
    a3
=a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}
return *this

Output:

1
2
3
Press any key to continue . . .

修改A& A:: operator=(A& rhs)

A& operator=(A& rhs)
{
    
//m_nMember1=rhs.GetMember1();
    return *this;
}

Output:

1
2
4
Press any key to continue . . .

2.当类中的成员变是常量时:

例如,当我们把上面那份代码中的class A的成员变量int m_nMember1;改成const int m_nMember1;前且注释掉SetMember中赋值语句。

void SetMember1(int nValue)
{
    
//m_nMember1=nValue;
}

 编译将出错: 

error C2512: 'A' : no appropriate default constructor available

 把代码改成:

#include <iostream>
using namespace std;

class A
{
public:
    A(
int nValue):m_nMember1(nValue){};
    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
        
//m_nMember1=nValue;
    }
private:
    
const int m_nMember1;
};
int main()
{
    A a1(
1);
    
//a1.SetMember1(1);
    cout<<a1.GetMember1()<<endl;

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl;

    a1.SetMember1(
3);
    A a3
=a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}

 Output:

1
1
1
Press any key to 
continue . . .

 

#include <iostream>
using namespace std; 

class A
{
public:
    A(
int& nValue):m_nMember1(nValue)
    {
    }

    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
//        m_nMember1=nValue;
    }

    
//A& operator=(A& rhs)
    
//{
    
//    //m_nMember1=rhs.GetMember1();
    
//    return *this;
    
//}
private:
    
const int m_nMember1;
}; 

int main()
{
    
int nValue=1;
    A a1(nValue);
    a1.SetMember1(
1);
    cout
<<a1.GetMember1()<<endl; 

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl; 

    a1.SetMember1(
3);
    
int nValue1=4;
    A a3(nValue1);
    a3
=a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}

编译出错:

error C2582: 'operator =' function is unavailable in 'A'

修改代码:

#include <iostream>
using namespace std; 

class A
{
public:
    A(
int& nValue):m_nMember1(nValue)
    {
    }

    
int GetMember1()
    {
        
return m_nMember1;
    }
    
void SetMember1(int nValue)
    {
//        m_nMember1=nValue;
    }

    A
& operator=(A& rhs)
    {
        
//m_nMember1=rhs.GetMember1();
        return *this;
    }
private:
    
const int m_nMember1;
}; 

int main()
{
    
int nValue=1;
    A a1(nValue);
    a1.SetMember1(
1);
    cout
<<a1.GetMember1()<<endl; 

    a1.SetMember1(
2);
    A a2(a1);
    cout
<<a2.GetMember1()<<endl; 

    a1.SetMember1(
3);
    
int nValue1=4;
    A a3(nValue1);
    a3
=a1;
    cout
<<a3.GetMember1()<<endl;
    
return EXIT_SUCCESS;
}

Output:

1
1
4
Press any key to continue . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值