Results 1 to 6 of 6

Thread: copy constructor Vs constructor

  1. #1
    Join Date
    Dec 2009
    Posts
    59

    copy constructor Vs constructor

    Hi friends,

    I am totally confused between the copy constructor and constructor. As per my knowledge the name of class and the name of constructor is same. The what is difference between copy constructor and constructor?. I have referred several books for this differentiation, but nothing was helpful. Do you know the difference between copy constructor and constructor? If yes then please let me know that. I will appreciate your help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: copy constructor Vs constructor

    A copy constructor accept a reference to it's own class, but while in normal constructor it is not possible. A constructor doesn't contains any return type. As with any method it is possible to override the constructors, that is to set several constructor with a number / type of different arguments. Thus, it is possible initialize the same object differently, depending on the construction method used.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: copy constructor Vs constructor

    The compiler does not generate only a constructor and destructor by default if the user has not defined. It also generates a copy constructor is called whenever a copy of object is performed in memory. The copy constructor is not always sufficient, it is sometimes necessary to provide it explicitly. Indeed, in the case of references or pointers, this is not the value of the data member itself is copied, but the value of the pointer to this data.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: copy constructor Vs constructor

    A constructor is a method that is called automatically whenever you create an object. This will be irrespective of the class allocation of the object: static, automatic ( stack ) Or dynamic. The compiler recognizes because it has the same name as the class. Beware, a manufacturer must return nothing. It would make no sense since it's establishment phase. A constructor is a method, therefore, when we create an object, we must explicitly call this method, using the usual syntax of function calls, that is to say by using parentheses.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: copy constructor Vs constructor

    The default constructor copy all the data members of the original object passed as arguments as data members of the new object. A copy of object is performed every time you pass an object by value to a function, or when a function returns an object the same way. By default the copy constructor use the copy of an object. The problem arises while using the copy constructor if the values of the original object is changed or if the object disappears.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: copy constructor Vs constructor

    Copy constructor: It is possible to create an object from another object (of course, the same class). This constructor expects the object parameter and copy each of these attributes to associate with its own attributes. The default behavior is a copy member to member.

    Constructor: Constructors are functions, and as such they obey the same rules of method overloading. The default constructor is a constructor that has no parameter. By default, this constructor does nothing. If you want a particular behavior, it will be necessary to redefine it.

Similar Threads

  1. Constructor vs Method
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 01:22 PM
  2. Replies: 3
    Last Post: 24-12-2009, 01:28 PM
  3. use of methods and constructor
    By beelow in forum Software Development
    Replies: 4
    Last Post: 14-11-2009, 01:37 AM
  4. Define Copy constructor in C++ code
    By Shaan12 in forum Software Development
    Replies: 3
    Last Post: 07-11-2009, 05:09 PM
  5. Overloading Assignment Operator and Copy constructor in C#
    By Japesh in forum Software Development
    Replies: 0
    Last Post: 14-01-2009, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,517,354.56183 seconds with 16 queries