Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   copy constructor (http://www.go4expert.com/forums/copy-constructor-t28280/)

pushpat 27Apr2012 12:32

copy constructor
 
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;
}


xpi0t0s 28Apr2012 14:59

Re: copy constructor
 
What compiler are you using (include full version number)?

pushpat 10May2012 10:07

Re: copy constructor
 
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.

Chong 10May2012 14:11

Re: copy constructor
 
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]

pushpat 10May2012 14:38

Re: copy constructor
 
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.

Chong 10May2012 16:13

Re: copy constructor
 
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

Chong 12May2012 11:45

Re: copy constructor
 
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

sumtaru 17May2012 19:30

Re: copy constructor
 
copy constructor is also called when you send an object by value to a function.

pushpat 4Jul2012 13:01

Re: copy constructor
 
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

sirama 5Jul2012 15:16

Re: copy constructor
 
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.


All times are GMT +5.5. The time now is 08:42.