Results 1 to 7 of 7
  1. #1
    shadow7 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default copy constructor

    im writing code for a project of mine. and i dont know what copy constructors really are.if i have a class , Dog,
    in that class i have lets say a constructor, public Dog(int size, int colour, string race){...}
    and i have a copy constructor, public Dog(Dog arg0){}
    what exactly does it do?

  2. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    it is called "overloading". Sometimes you have objects that can be created with different types of constructors.
    An example could be Javas ArrayList.

    Constructor Summary
    ArrayList()
    Constructs an empty list with an initial capacity of ten.
    ArrayList(Collection c)
    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
    ArrayList(int initialCapacity)
    Constructs an empty list with the specified initial capacity.

    It adds flexibility.
    It is up to you to decide what the second constructor should do in your Dog class

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Wrong language. A copy constructor is a C++ language feature.

    If you are attempting to copy an object, why not try:
    Dog a = new Dog();
    Dog b = a.clone();

    Clone is inherited from the Object class. A word of warning though -- the clone() method will only work if you have implemented it. Many classes in the java api have done so, but if you are attempting to clone a class you defined, you will need to implement the cloning yourself using the Cloneable interface.

    More to your question: 'What does it do?'
    If I understand the C++ feature correctly Copy Constructor(I am not a C++ programmer), then this is a way to clone an object. For example:

    Object a = new Object();
    Object b = a;
    Then the result of:
    a == b
    is true.

    But when cloning:

    Object a = new Object();
    Object b = a.clone();
    Then the result of:
    a == b
    is false.

    In the first case, (b) is a reference to (a). In the second case, (b) is a new object which contains the same data as (a) but is not actually (a) itself, rather, a copy or clone.

    Here is a link to a tutorial for implementing cloneable in java:
    Java Tips - How to implement Cloneable Interface

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    it is called "overloading"
    Thats what I thought he was talking about at first too, but copy constructors are a real thing in C++.

  5. #5
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Copy constractor

    this is also a type of overloading, where we assign a all the parameter values(object reference) to a another constructor for creating a exact copy of the object.....!

  6. #6
    shadow7 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    so, public Dog(Dog mike(4, 3, bulldog)){}
    will create what?
    "public Dog(Dog mike(4, 3, bulldog)){}" == "Dog mike = new Dog(4,3, bulldog);" ???

  7. #7
    shadow7 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    sorry,
    if "Dog mike = new Dog(4,3, bulldog);"
    so, public Dog(Dog mike){}
    will create what?
    "public Dog(Dog mike){}" == "Dog mike = new Dog(4,3, bulldog);" ???

Similar Threads

  1. How to copy a polygon
    By cassysumandak in forum New To Java
    Replies: 6
    Last Post: 10-07-2009, 12:37 AM
  2. How to make a copy of a set
    By ZebV43 in forum New To Java
    Replies: 3
    Last Post: 06-12-2009, 07:45 AM
  3. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 06:31 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •