Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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;
}
Posted

1 solution

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

The above function would be called in line 2 of your main()
 
Share this answer
 
Comments
CPallini 12-Mar-14 7:47am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900