copy constructor

pushpat's Avatar, Join Date: Feb 2012
Go4Expert Member
Hi ,

I wrote sample (below) program to check when copy constructor and assignment operator called.
my doubt was if object a, b defined previous to statement a(b), then will a(b) call copy c_tor or assignment operator and why below program giving compilation error(mentioned inline) . Can anybody tell me how to fix the issue.

I have tried using test(test& y) , still compiler gives "
no matching function for call to ‘test::test(test)’"



Code:
#include<iostream>
using namespace std;


class test
{
	int i;
	public:
	test(int x=0):i(x){}
	~test(){}
    test(const test& y)
	{
		cout<<"cp_ctor"<<endl;
		
	}
	test& operator=(test& m)
	{
		cout<<"assingment"<<endl;
		return *this;
	}
		
};
test sentobject(void)
{
	test temp(88);
	cout<<"sending..";
	return temp;
}
int main()
{
	test a(3),b(6);
	
        cout<<"a(b)calls";
	a(b);//error: no match for call to ‘(test) (test&)’
	

        cout<<"sendobject calls"<<endl;
	test k = sentobject();

	return 0;
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What compiler are you using (include full version number)?
0
pushpat's Avatar, Join Date: Feb 2012
Go4Expert Member
I am using g++ compiler

g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi
trying the following:
Code:
int main()
{
    //test a(3),b(6);
    test b(6);
    cout<<"a(b)calls";
    test a(b);
    cout<<"sendobject calls"<<endl;
    test k = sentobject();
    return 0;
}
[/quote]
0
pushpat's Avatar, Join Date: Feb 2012
Go4Expert Member
Thank you Chong,

In code suggested, test a(b) will call copy c_tor.
But I wanted to know particular case of a(b) where both a and b pre-defined . According to my knowledge it will call assignment operator so I wrote this(posted) program.
0
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi pushpat
I have limited experience of C++ but I think that you should have "a=b;" instead of "a(b);" to use the assignment operator in your posted program!!? With a(b), my MS VC++ wont compile it!!
Best regards
Chong

Last edited by Chong; 10May2012 at 16:20..
0
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi pushpat
By the way, what made you think that a(b) will call call a copy constructor or assigment operator. As far as I know, once the object is made, it can not be remade once agian as a(b).
best regards
Chong
0
sumtaru's Avatar
Light Poster
copy constructor is also called when you send an object by value to a function.
0
pushpat's Avatar, Join Date: Feb 2012
Go4Expert Member
Hi Chong,
According to my knowledge assignment operator called when we are assigning object to existing objects
and copy c_tor called copying object to non-existing object(while creating object) . so I wrote sample program which is posted here to understand.

Thanks
Pushpa.T
0
sirama's Avatar, Join Date: Jun 2012
Light Poster
Constructor for an object called, to initialize the object. It is a two step process in C++
1) Allocates the Space 2) Initializes the allocated space by providing initial values to its data members

Copy constructor is called when you Construct an Object from the already initialized one. In your case a(b), both are initialized object, and there is no need for construction at all. So why do you think it should call copy constructor. A(b) does't use any =, so assignment operator also not called. Finally the compiler decides a() as member function. But, you don't provide one and that's why you see a error message.