c++类的拷贝构造函数、隐式转换、重载的赋值操作符的一些分析和实例

1. 隐式转换和拷贝构造之间的一点实例分析

#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

class A
{
public:
	A(int i): m_a(i) 
	{
		printf("constructor\n");
	}

	A(const A &a)
	{
		printf("copy constructor\n");
		m_a = a.m_a;
	}

protected:
	int m_a;
};

int _tmain(int argc, _TCHAR* argv[])
{
        A a = 1;
        getchar();
	return 0;
}
运行结果是:

constructor
分析:从以上结果可以发现通过隐式转换A a = 1相当于执行了A a(1)。这与我预测的先把1转换为一个A类型的临时变量调用一次构造函数,然后临时变量再赋值给a调用一次拷贝构造函数的想法不一样,出现这种结果应该是编译器编译的时候发现存在隐式转换于是直接把A a = 1处理成A a(1)了,这样可以省掉一些中间不必要的开销。


2. 重载的赋值操作符、拷贝构造函数与隐式转换

#include <stdio.h>

class A
{
public:
	A(int i): m_a(i) 
	{
		printf("constructor\n");
	}

	A(const A &a)
	{
		printf("copy constructor\n");
		m_a = a.m_a;
	}

	A& operator = (const A& a)
	{
		printf("operator =\n");
		this->m_a = a.m_a;
		return *this;
	}

protected:
	int m_a;
};

int _tmain(int argc, _TCHAR* argv[])
{
	A a(1);
	a = 1;
	getchar();
	return 0;
}

运行结果如下:

constructor
constructor
operator =
分析:首先是定义a的时候调用了一次构造函数,第二次构造函数是由于 a = 1的时候因为隐式转换把1转换为A类型的临时变量导致的,最后是赋值操作


3. 如何避免隐式转换

#include <stdio.h>

class A
{
public:
	A(int i): m_a(i) 
	{
		printf("constructor\n");
	}

	A(const A &a)
	{
		printf("copy constructor\n");
		m_a = a.m_a;
	}

protected:
	int m_a;
};

int _tmain(int argc, _TCHAR* argv[])
{
	A a = 1;
	getchar();
	return 0;
}

分析:通过给引发隐式转换的构造函数加上explicit关键字来阻止隐式转换的发生,上面代码编译不能通过:error C2440: “初始化”: 无法从“int”转换为“A”。


4.重载+操作符:

#include <stdio.h>

class A
{
public:
	explicit A(int i) : m_a(i)
	{
		printf("constructor\n");
	}

	A(const A &a)
	{
		printf("copy constructor\n");
		m_a = a.m_a;
	}

	A& operator = (const A& a)
	{
		printf("operator =\n");
		this->m_a = a.m_a;
		return *this;
	}

	A operator + (const A& a)
	{
		printf("Generic\n");
		return A(this->m_a + a.m_a);
	}
	
	A operator + (const int a)
	{
		printf("int\n");
		return A(this->m_a + a);
	}

	double operator + (const double a)
	{
		printf("double\n");
		return this->m_a + a;
	}

protected:
	int m_a;
};

int _tmain(int argc, _TCHAR* argv[])
{
	A a(1);
	printf("\n");
	A b = a + 1;
	printf("\n");
	double c = a + 2.0;    //出错:double c = a + 2;
	printf("\n");
	A d = a + b;
	getchar();
	return 0;
}

运行结果如下:

constructor

int
constructor

double

Generic
constructor
分析:从以上结果可知,类A可以重载多个operator + 操作,传入参数和返回值可以根据自己的需要去设置。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值