AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: leena — October 16, 2006
    -11 votes
      + -

    copy constructor creates new object while the assignmetn opertor used for existing objects.

  2. Submitted By: Ayush Sharma — October 17, 2006
    +26 votes
      + -

    Copy contructor is a special type of constructor used in C++ language to create a copy of object. If we dont explicitly declare, the compiler creates one by default. This default constructor doesnt do much except for byte-by-byte copy. In special cases, where class owns a pointer or file-descriptor it is not generally recommended to have byte-by-byte copy. In such cases, developer has to create a constructor customized for the use.

    For example, in case Class A has a member which is pointer to an int. If default copy-constructor is used,
    A obj2 = obj1 ;
    obj2’s member pointer will point to the same memory location pointed by obj1’s pointer. This can later cause problems like memory leak, or dangling pointer.
    So in such cases, if developer writes his own copy-constructor and makes sure that obj2’s pointer now pointes to a diff memory location which has the same data value as in obj1’s pointer.

    A::A (const A&) {
    int *ptrMember = new int ;
    *ptrMember= *A.ptrMember
    }

  3. Submitted By: Madhura — December 12, 2006
    +12 votes
      + -

    The difference between the copy constructor and assignment operator is that the copy constructor is a constructor — a function whose job it is to turn raw storage into an object of a specific class. An assignment operator, on the other hand, copies state between two existing objects. In other words, an assignment operator has to take into account the current state of the object when copying the other object’s state into it. The copy constructor is creating a new object from raw storage and knows it’s writing over garbage.

  4. Submitted By: Contibutor — January 4, 2007
    +19 votes
      + -

    Solution by Ayush does not give the answer to the question rather it explains the copy constructor.

    Basic difference between copy constructor and assignment operator is the time when it is used..

    Copy constructor as name suggests is used when the object is created. And it copies the contents of another object of same type byte by byte to its member variables.

    On the other hand assignment operator which does the same operation as the copy constructor but can be called anytime in the life of the program..

    I disagree with madhura on the statement that “assignment operator has to take into account teh current state of the object”.. No assigment operation ever takes into account the surrent state of the object.. If there is valid memory created for the object (which would be true, since you are calling assignment operation) it just overwrites the contents of the member variables..

    An assignment operator can also write in a storage which is garbage.. Consider a class which only has default constructor provided by the compiler.. When an object is created mem is allocated but members are not initialised.. In that case when you perform an assignment operation on the object, it is writing over garbage..

  5. Submitted By: Contibutor — January 4, 2007
    +8 votes
      + -

    Also copy contructors are called when you call a function passing objects by value or returning by value.

  6. Submitted By: Chini — January 30, 2007
    +0 votes
      + -

    I dont understand why 9 votes to first answer, am not saying its wrong, but its not the explaination for question. And regarding the problem he reported (byte by byte), we will have that problem everywhere we try to copy byte by byte the variable or objects which is treated or created as ponter

  7. Submitted By: Robert — April 30, 2007
    +2 votes
      + -

    Assignment operator will not create the object for u,whereas copyconstructor will create a object for u.

    And moreover copyconstructor will copy the entire object. coming to the assignment operator will just refering the object it is not exactly contain the true contact just it will give reference.

  8. Submitted By: hicks — May 19, 2007
    +2 votes
      + -

    When Madhura (4) says “an assignment operator has to take into account the current state of the object when copying the other object’s state into it”, that is correct because existing members may need to be deleted or deconstructed.

  9. Submitted By: hicks — May 19, 2007
    +0 votes
      + -

    If I might add a little more ….
    assertion::
    An assignment operator can always be wirtten
    as a destructor followed by a copy constructor.
    E.g.
    class A
    {
    A(const A& a) { /* deep copy */ }
    operator=(const A& a)
    {
    this->~A();
    new(this) A(a);
    }
    }

  10. Submitted By: hicks — May 19, 2007
    +1 votes
      + -

    Sorry, the assignment operator declaration syntax should have been
    A& operator=(const A& a)

  11. Submitted By: padmakumar — February 18, 2008
    not yet rated
      + -

    Copy constuctor is special type of constructor, that copy the address of the previous obj;

    For Ex: using in console application:

    class staff
    {
    int Stid; string post;
    public staff() // Default constructor
    {
    Console.WriteLine(”Enter the staff id:”);
    Stid = int.Parse(Console.ReadLine());
    Console.WriteLine(”Enter the staff name:”);
    post = Console.ReadLine();
    }
    public staff(staff copy) // copy constructor
    {
    Stid = copy.Stid;
    position = copy.position;
    }
    public void display()
    {
    Console.WriteLine(” staff id:” + Stid);
    Console.WriteLine(”staff position:” + position);
    Console.ReadLine();
    }

  12. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.