1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

copy constructor

Discussion in 'C++' started by pushpat, Apr 27, 2012.

  1. pushpat

    pushpat New 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;
    }
    
     
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    What compiler are you using (include full version number)?
     
  3. pushpat

    pushpat New 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.
     
  4. Chong

    Chong New 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]
     
  5. pushpat

    pushpat New 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.
     
  6. Chong

    Chong New 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: May 10, 2012
  7. Chong

    Chong New 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
     
  8. sumtaru

    sumtaru New Member

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

    pushpat New 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
     
  10. sirama

    sirama New Member

    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.
     

Share This Page