Why overloaded copy constructor is not getting called here?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • subi09
    New Member
    • Nov 2012
    • 6

    Why overloaded copy constructor is not getting called here?

    Code:
    #include<iostream>
    using namespace std;
    class Cents
    {
    
    public:
      int m_nCents;
       Cents(int nCents=0):m_nCents(nCents)
        {
           cout<<"Calling normal constructor with value:";   m_nCents = nCents;
           cout<<m_nCents<<endl;
        }
    
        // Copy constructor
        Cents(const Cents& cSource)
        {
            cout<<"Calling copy construct\n";
            m_nCents = cSource.m_nCents;
           std::cout<<m_nCents<<std::endl;
        }
    };
    
    
    int main(){
    
    
    Cents obj2 = 37;
    cout<<"Value of m_nCents with sobject2:"<<obj2.m_nCents;
    
    return 0;
    }

    o/p is
    Calling normal constructor with value:37
    Value of m_nCents with sobject2:37

    Question is :
    Why is the overloaded copy constructor that I have written not getting called here?Internally default copy constructor is getting called.Thats why we get
    value of obj2.m_nCents as 37.
    ~
    ~
    Last edited by Meetee; Nov 19 '12, 01:21 PM. Reason: please add code tags <code/> around your code
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    In order for a copy constructor to be called, there must be an object to copy. In your example you are creating an object and initializing it with 37. That would be a call to a constructor that has an int argument rather than a Cents& argument.

    Comment

    • rakesh1225
      New Member
      • Mar 2012
      • 13

      #3
      If you want to call a copy constructor you need to write the below code in your main function.

      Cents obj1=37;

      Cents obj2=obj1;
      (or)
      Cents obj2(obj1);

      cout<<"Value of m_nCents with sobject2:"<<obj 2.m_nCents;


      Output:

      Value of m_nCents with sobject2:37


      Copy Constructor will be called at the time of initialisation of object with another object of same class type.

      Comment

      • subi09
        New Member
        • Nov 2012
        • 6

        #4
        Thanks for the reply .But as per my understanding,

        Cents obj2 = 37;=> statement will create a temporary object of Cents and initialise it with 37(that is where constructor is called) and then it will copy the value of that object to obj2(at this time copy constructor should be called).



        Please clarify this and correct me if am wrong.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You are not correct. There is this constructor:

          Code:
          Cents(int nCents=0):m_nCents(nCents)
          This already has an int argument. The compiler will pick this one as the better fit.

          Comment

          • subi09
            New Member
            • Nov 2012
            • 6

            #6
            Then if am adding 2 piece of code extra to the above like below

            Code:
            #include<iostream>
            using namespace std;
            class Cents
            {
            
            public:
            int m_nCents;
            Cents(int nCents=0):m_nCents(nCents)
            {
            cout<<"Calling normal constructor with value:"; m_nCents = nCents;
            cout<<m_nCents<<endl;
            }
            
            // Copy constructor
            Cents(const Cents& cSource)
            {
            cout<<"Calling copy construct\n";
            m_nCents = cSource.m_nCents;
            std::cout<<m_nCents<<std::endl;
            }
            friend void display(Cents obj);-->newly added
            };
            
            void display(Cents obj){}
            
            
            int main(){
            
            //Cents obj2 = 37;
            display(37);---> newly added
            // cout<<"Value of m_nCents with sobject2:"<<obj2.m_nCents;
            
            return 0;
            }

            O/p>
            Calling normal constructor with value:37
            Calling copy construct
            37

            Question is

            then why in this case. display(37),fir st calls constructor,the n copy constructor.As per the above explanation,it should stop with call to constructor.Thi s scenario actually is the root cause of my above doubt.It would be very helpful if you can clear this.
            Last edited by Rabbit; Nov 20 '12, 05:59 PM. Reason: Please use code tags when posting code.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              I have no idea.

              The display(37) should call Cents::Cents(in t). That will initialize the Cents object created as the function argument. There will be no copy constructor call.

              I did compile and link your code and ran it using Visual Studio.NET 2008 and there is just the Cents::Cents(in t) call. The copy constructor is not called.

              Comment

              Working...