Results 1 to 4 of 4
-
Re: What is the point of a copy constructor?
You need to clarify your question because it is rather confusing, and you should paste the relevant code in the post rather than paste a link to your code.
What I've made of your question is this: What happens if the parameter objects Instructor inst and TextBook text are not initialised when you create a new Course object. The answer is that they will become a null pointer until they are initialised. If you try to access methods or fields before the object is initialised it will cause a NullPointerException.
I don't know what you mean by a 'copy' constructor. Perhaps you mean this:Java Code://some code to try out your class ... Instructor instructor1; //not initialiased: value is 'null' TextBook text1; //null Course science = new Course("Science", instructor1, text1); ... //the Course class class Course { //variables ... public Course(String name, Instructor inst, TextBook text) { //try to access the fields instructorName = inst.getLastName(); //NullPointerException is thrown }
If thats what you mean, it is known as Polymorphism in Java and maybe other languages.Java Code:class SomeClass { SomeClass() { //a blank constructor } SomeClass(String str) { //another constructor someVariable = str; } }
Both methods are distinguished by their 'method signature' - The first one has no params, but the second one has a String param.
So long as you have different combinations of parameters you can create as many as you like to suit your convenience.
- 01-04-2012, 10:38 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: What is the point of a copy constructor?
I think the question might getting at the fact that someone with an instance of one course could get the textbook and set that textbook's title to something else. Likewise they could give the instructor a new and amusing name. The point is that these changes be seen in all the courses involving this textbook or instructor if they weren't "defensive" and clone the instances.My question is this: What problems might occur if the constructor of the Course class does not call the
copy constructors for its instructor and textbook instance variables?
Copy constructors are no panacea! What problems might occur if the constructor of the Course class does call the copy constructors for its instructor and textbook instance variables?
- 01-05-2012, 09:54 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: What is the point of a copy constructor?
To clarify the question, what is the difference between the two implementations of the Course constructors
Java Code:public Course(String name, Instructor inst, Textbook text){ courseName = name; instructor = new Instructor(inst); textBook = new Textbook(text); }All i found was that it results in unnecessary object creation which means less pointers and I don't really see how that would help even on a large scaleJava Code:public Course(String name, Instructor inst, Textbook text){ courseName = name; instructor = inst; textBook = text; }
- 01-05-2012, 10:59 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: What is the point of a copy constructor?
pbrockway has pretty much explained that, though.
The implication of the first constructor is that the Course should not be able to make changes to the original Instructor or TextBook objects. However, in that instance I would have made both of them immutable and passed those in instead.
You would normally see a copy constructor in getters, to be honest.
which will ensure anyone getting the date cannot change the value of it in my object.Java Code:Date getDate() { return new Date(date.getTime()); }
Similar Threads
-
How to call a master constructor from a minor constructor?
By b.m in forum New To JavaReplies: 5Last Post: 12-14-2011, 01:47 PM -
array copy in class constructor.
By Juukamen in forum New To JavaReplies: 2Last Post: 10-29-2011, 12:07 AM -
ISSUE: Array as class constructor will not copy elements, only reference
By Lucificate in forum New To JavaReplies: 16Last Post: 07-08-2010, 09:13 PM -
copy constructor
By shadow7 in forum New To JavaReplies: 6Last Post: 12-29-2009, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks