-
March 9th, 2009, 04:46 PM
#1
need some help with copy constuctor
Code:
class Parent {
int i;
public:
Parent(int ii) : i(ii) {
cout << "Parent(int ii)\n";
}
Parent(const Parent& b) : i(b.i) {
cout << "Parent(const Parent&)\n";
}
Parent() : i(0) { cout << "Parent()\n"; }
friend ostream&
operator<<(ostream& os, const Parent& b) {
return os << "Parent: " << b.i << endl;
}
};
Hi,
Im having difficulty in understanding copy constructors so the operator overloading.can any one explain me in detail the following program and how copy constructor works.I also dont understand why b.i is used in the constructor initializer list.
Thanks in advance,
-
March 9th, 2009, 11:49 PM
#2
Re: need some help with copy constuctor
It is used to copy one object to another.
This means copying the data members of the class.
So if you call it like so -
Parent p1(100);
Parent p2(p1);
The second statement tells that p2 must be copied from p1.
The only data member of the class is i.
That is why b.i is used. b represents the object p1.
-
March 12th, 2009, 08:24 PM
#3
Re: need some help with copy constuctor
thank you for the reply.For example in operator overloading "+"they use i+b.i" does it mean current val +prev value??
-
March 12th, 2009, 10:34 PM
#4
Re: need some help with copy constuctor
Originally Posted by brett01
For example in operator overloading "+"they use i+b.i" does it mean current val +prev value??
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.
-
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;
}
-
March 12th, 2009, 11:05 PM
#6
Re: need some help with copy constuctor
Originally Posted by brett01
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.
You declare a Parent object named b. Since Parent has an i member, you can then use b.i.
-
March 12th, 2009, 11:07 PM
#7
Re: need some help with copy constuctor
Originally Posted by laserlight
You declare a Parent object named b. Since Parent has an i member, you can then use b.i.
Is there any advantage for declaring "b.i" ??
-
March 12th, 2009, 11:12 PM
#8
Re: need some help with copy constuctor
Originally Posted by brett01
Is there any advantage for declaring "b.i" ?
Advantage over what? Since the compiler generated copy constructor will suffice, there is no advantage in implementing it yourself.
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
|