Xiangliang Meng wrote:[color=blue]
>
> The fragment code:
>
> #include <iostream>
> using namespace std;
>
> class Integer {
> int i;
> public:
> Integer() : i(0) {};
>
> Integer(int I) : i(I) {};
> Integer(Integer & other) : i(other.i) {} // line 10
> Integer & operator=(Integer &other) {
> i = other.i;
> return (* this);
> }
> Integer & operator=(int a) {
> i = a;
> return (* this);
> }
> friend ostream& operator<<(ostream &os, Integer &I) {
> os << I.i;
> return os;
> }
> };
>
> int main() {
> int a = 5;
> Integer Int1 = a; // line 27; error happens here.
> Integer Int2 = Int1; // line 28;
>
> cout << Int1 <<", " << Int2 << endl;
> Int1 = 5; // line 31
> cout << Int1 <<", " << Int2 << endl;
> Int2 = Int1; // line 33
> cout << Int1 <<", " << Int2 << endl;
> }
>
> case 1: If the line 10 is there, compiling aborts at line 27.
> g++.exe "Integer.cpp" -o
> Integer.exe" -g3 -I"d:\Dev-Cpp\include\c++" -I"d:\Dev-Cpp\include\c++\mi
> ngw32" -I"d:\Dev-Cpp\include\c++\backward" -I"d:\Dev-Cpp\include" -L"d:\
> Dev-Cpp\lib"
> Integer.cpp: In function `int main()':
> Integer.cpp:27: no matching function for call to `Integer::Integer(Integer)'
> Integer.cpp:10: candidates are: Integer::Integer(Integer&)
> Integer.cpp:9: Integer::Integer(int)
> Integer.cpp:27: initializing temporary from result of `
>
> case 2: If the line 10 is delete, there is no error at line 27 when
> compiling.
>
> Could someone explain this for me?[/color]
Integer Int1 = a; // line 27; error happens here.
Here a temporary Integer object is created from a. This temporary
is then feed into the copy constructor to create Int1.
The signature of your copy constructor is:
Integer(Integer & other) : i(other.i) {} // line 10
But a temporary cannot be bound to a reference which is not
const! Thus the error.
If you don't write the copy constructor yourself, the compiler
generates one. But the compiler generated has the signature
Integer( const Integer & other) : i(other.i) {} // line 10
See the difference?
This difference is, why the compiler generated one works, while yours
doesn't.
[color=blue]
>
> What does line 27 do in detail? Invoke Integer::Integer(int I) directly? or
> Create a temporary Integer object from the variable 'a' and use
> copy-constructor to create Int1 from this termporary object?[/color]
The later one.
[color=blue]
>
> Could someone explain more in details on line 27, 28, 31 and 33? What's the
> difference among them?[/color]
27: Integer Int1 = a;
object gets created -> constructor is used,
Thus 'a' first has to be transformed into an Integer
object, use the ctor which takes an int for that.
28: Integer Int2 = Int1;
object gets created -> constructor is used
Since Int1 is already an Integer object, use the copy
constructor for Int2
31: Int1 = 5;
This is an assignment. Thus an opertor= is used.
First try to find something that allows an int to be assigned
directly to an Integer ->
Integer & operator=(int a)
is found and used
33: Int2 = Int1;
This is an assignment. Thus an operator= is used.
Find an operator= which can take an Integer object ->
Integer & operator=(Integer &other)
is found and used.
--
Karl Heinz Buchegger
kbuchegg@gascad.at