need some help with copy constuctor
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: need some help with copy constuctor

  1. #1
    Join Date
    Oct 2008
    Location
    San Francisco
    Posts
    60

    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,

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    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.
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Oct 2008
    Location
    San Francisco
    Posts
    60

    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??

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    Re: need some help with copy constuctor

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Oct 2008
    Location
    San Francisco
    Posts
    60

    Re: need some help with copy constuctor

    Quote Originally Posted by laserlight View Post
    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;
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    Re: need some help with copy constuctor

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Oct 2008
    Location
    San Francisco
    Posts
    60

    Re: need some help with copy constuctor

    Quote Originally Posted by laserlight View Post
    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" ??

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    Re: need some help with copy constuctor

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center