C ++中的CopyConstructor [英] CopyConstructor in c++

查看:86
本文介绍了C ++中的CopyConstructor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Copy Constructor编写了示例应用程序但是没有调用Copy构造函数。



任何人都告诉我,这段代码有什么问题?



示例:

I have wrote sample application for Copy Constructor but Copy constructor is not getting called.

anybody tell me , what is wrong in this code?

Example:

#include <iostream>
#include <vector>

class Copy
{
	int m_a;
public:
	Copy()
	{
		std::cout<<"Const"<<std::endl;
	}
	Copy(int c)
	{
		m_a = c;
	}
	~Copy()
	{
		std::cout<<"Dest"<<std::endl;
	}
	void func(const Copy &c)
	{
		m_a = c.m_a;

	}

	int Result()
	{

		return m_a;
	}
};


int main()
{
	Copy c(10);
	Copy c2=c;
	int a = c2.Result();
	std::cout<<a<<std::endl;
	return 0;
}

推荐答案

代码中没有复制构造函数。它应该具有以下签名:

There is no copy constructor in your code. It should have the following signature:
Copy(const Copy& other)



以上函数将在 main()的第2行调用


这篇关于C ++中的CopyConstructor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆