Computer Science - Object Oriented Programming Using C++ - Discussion
Discussion Forum : Object Oriented Programming Using C++ - Section 1 (Q.No. 11)
11.
When the compiler cannot differentiate between two overloaded constructors, they are called
Discussion:
2 comments Page 1 of 1.
Harpreet said:
1 decade ago
Please give some examples.
Xoxo said:
1 decade ago
^to the one who can't google this.
The problem exists when you try to instantiate a class that could apply to more than one constructor.
For example:
public Example(String name) {
this.name = name;
}
public Example(SomeOther other) {
this.other = other;
}
If you call the constructor with a String object, there's one definite constructor. However, if you instantiate new Example(null) then it could apply to either and is therefore ambiguous.
The same can apply to methods with similar signatures.
The problem exists when you try to instantiate a class that could apply to more than one constructor.
For example:
public Example(String name) {
this.name = name;
}
public Example(SomeOther other) {
this.other = other;
}
If you call the constructor with a String object, there's one definite constructor. However, if you instantiate new Example(null) then it could apply to either and is therefore ambiguous.
The same can apply to methods with similar signatures.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers