copy constructor

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

  1. pushpat

    pushpat New Member

    Joined:
    Feb 14, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What compiler are you using (include full version number)?
     
  3. pushpat

    pushpat New Member

    Joined:
    Feb 14, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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

    Joined:
    Feb 14, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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

    Joined:
    May 17, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    copy constructor is also called when you send an object by value to a function.
     
  9. pushpat

    pushpat New Member

    Joined:
    Feb 14, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jun 18, 2012
    Messages:
    7
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Systems Er.
    Home Page:
    http://www.mstecharticles.com
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice