|
-
March 12th, 2009, 10:46 PM
#5
Re: need some help with copy constuctor
 Originally Posted by laserlight
It means "perform some operation on i and b.i". Hopefully that operation has something akin with addition, but it could also say... test for equality, print a novel, etc. In the context of your question, the result of that operation is probably used to initialise a member variable, bit that is unusual for a copy constructor since the copy is usually identical in the values of its members (or the value of what they point to, in the case of pointer members) with the original.
Hi thank you for the reply mate.What i dont understand is that since constructor is always associated with the object so generally we declare only "i" and not "b.i" but when it comes to copy constructor why do we declare b.i.For example in the following code
Code:
class Integer {
int i;
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const {
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer&
operator+=(const Integer& rv) {
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
};
int main() {
cout << "built-in types:" << endl;
int i = 1, j = 2, k = 3;
k += i + j;
cout << "user-defined types:" << endl;
Integer ii(1), jj(2), kk(3);
kk += ii + jj;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|