Results 1 to 7 of 7
Thread: copy constructor
- 12-29-2009, 06:13 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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?
- 12-29-2009, 06:28 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
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
- 12-29-2009, 06:33 PM #3
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
- 12-29-2009, 06:34 PM #4
Thats what I thought he was talking about at first too, but copy constructors are a real thing in C++.it is called "overloading"
- 12-29-2009, 06:48 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
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.....!
- 12-29-2009, 06:54 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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);" ???
- 12-29-2009, 06:56 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to copy a polygon
By cassysumandak in forum New To JavaReplies: 6Last Post: 10-07-2009, 12:37 AM -
How to make a copy of a set
By ZebV43 in forum New To JavaReplies: 3Last Post: 06-12-2009, 07:45 AM -
Deep copy?!
By deepthought015 in forum New To JavaReplies: 3Last Post: 05-07-2009, 06:31 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks