Deep versus Shallow Copy - Part 2

Discussion in 'C++' started by blangela, Nov 26, 2006.

  1. blangela

    blangela Guest

    2.0 Sample Code

    class ABC // dummy class used below
    {};

    class Example2
    {
    public:
    Example2(); // default ctor
    Example2( const Example2 &); // copy ctor
    Example2 & operator = (const Example2 &); // overloaded
    // assignment operator
    ~Example2(); //dtor
    private:
    int ii;
    double dd;
    ABC * abcPtr; // will point to a dynamically created ABC object
    };


    // default ctor
    Example2::Example2()
    {
    abcPtr = new ABC; // dynamically create an ABC object and have
    // the class member abcPtr point to it.
    ii = 10; // assign some value
    dd = 3.765; // assign some value
    }


    // copy ctor
    Example2::Example2(const Example2 & src)
    :ii(src.ii),dd(src.dd) // MIL
    {
    abcPtr = new ABC; // Same as default ctor above.
    *abcPtr = *(src.abcPtr); // Do a deep copy.
    }


    // dtor
    Example2::~Example2()
    {
    delete abcPtr; //Destroy the dynamically created ABC object.
    abcPtr = 0; //Not necessary, but prevents a calamity if an
    // attempt is made to destroy the same
    // ABC object a second time.
    }

    // overloaded assignement operator
    Example2 & Example2::eek:perator = (const Example2 & RHS)
    {
    //First check that the operand on the Right Hand Side (RHS)
    // of the assignment operator is not the exact same object
    // as the operand on the LHS of the assignment operator.
    // We do this by testing to see if both objects have the
    // same address!

    if (this == &RHS) // if true, we are done!
    {
    return *this; // return the original LHS operand
    }
    else // we still have some work to do
    {
    ii = RHS.ii; // a shallow copy is good enough here
    dd = RHS.dd; // here as well

    *abcPtr = *(RHS.abcPtr); // here we need a deep copy.

    return *this; // return the modified LHS operand.
    }
    }


    // Simple program to test our Example2 class
    int main()
    {
    Example2 ex2_1, ex2_2; // Will invoke default ctor (twice).

    ex2_1 = ex2_2; // Will invoke overloaded Assignment operator.

    Example2 ex2_3(ex2_1); // Will invoke copy ctor.

    return 0; // We are finished!

    } // end of main() - Example2 dtor will be invoked 3 times
     
    blangela, Nov 26, 2006
    #1
    1. Advertising

  2. blangela

    peter koch Guest

    blangela skrev:
    > 2.0 Sample Code
    >
    > class ABC // dummy class used below
    > {};
    >
    > class Example2
    > {
    > public:
    > Example2(); // default ctor
    > Example2( const Example2 &); // copy ctor
    > Example2 & operator = (const Example2 &); // overloaded
    > // assignment operator
    > ~Example2(); //dtor
    > private:
    > int ii;
    > double dd;
    > ABC * abcPtr; // will point to a dynamically created ABC object
    > };
    >
    >
    > // default ctor
    > Example2::Example2()
    > {
    > abcPtr = new ABC; // dynamically create an ABC object and have
    > // the class member abcPtr point to it.
    > ii = 10; // assign some value
    > dd = 3.765; // assign some value
    > }
    >
    >
    > // copy ctor
    > Example2::Example2(const Example2 & src)
    > :ii(src.ii),dd(src.dd) // MIL
    > {
    > abcPtr = new ABC; // Same as default ctor above.
    > *abcPtr = *(src.abcPtr); // Do a deep copy.
    > }
    >
    >
    > // dtor
    > Example2::~Example2()
    > {
    > delete abcPtr; //Destroy the dynamically created ABC object.
    > abcPtr = 0; //Not necessary, but prevents a calamity if an
    > // attempt is made to destroy the same
    > // ABC object a second time.
    > }
    >
    > // overloaded assignement operator
    > Example2 & Example2::eek:perator = (const Example2 & RHS)
    > {
    > //First check that the operand on the Right Hand Side (RHS)
    > // of the assignment operator is not the exact same object
    > // as the operand on the LHS of the assignment operator.
    > // We do this by testing to see if both objects have the
    > // same address!
    >
    > if (this == &RHS) // if true, we are done!
    > {
    > return *this; // return the original LHS operand
    > }
    > else // we still have some work to do
    > {
    > ii = RHS.ii; // a shallow copy is good enough here
    > dd = RHS.dd; // here as well
    >
    > *abcPtr = *(RHS.abcPtr); // here we need a deep copy.
    >
    > return *this; // return the modified LHS operand.
    > }
    > }
    >
    >
    > // Simple program to test our Example2 class
    > int main()
    > {
    > Example2 ex2_1, ex2_2; // Will invoke default ctor (twice).
    >
    > ex2_1 = ex2_2; // Will invoke overloaded Assignment operator.
    >
    > Example2 ex2_3(ex2_1); // Will invoke copy ctor.
    >
    > return 0; // We are finished!
    >
    > } // end of main() - Example2 dtor will be invoked 3 times


    I believe that the code above is a typical example of real code, but
    anyway it is bad code. A rule of thumb is that code in a class should
    be centered around the purpose of the class itself ("the class, the
    whole class and nothing but the class"), and the above code is mainly
    centered on copying a pointer!
    My recommendation is that you learn your students (basic) template
    programming before pointers. This should enable your students to write
    a safe-pointer template for the class above without obfuscating
    anything.

    Kind regards
    Peter
     
    peter koch, Nov 26, 2006
    #2
    1. Advertising

  3. blangela

    blangela Guest

    peter koch wrote:
    > blangela skrev:
    > > 2.0 Sample Code
    > >
    > > class ABC // dummy class used below
    > > {};
    > >
    > > class Example2
    > > {
    > > public:
    > > Example2(); // default ctor
    > > Example2( const Example2 &); // copy ctor
    > > Example2 & operator = (const Example2 &); // overloaded
    > > // assignment operator
    > > ~Example2(); //dtor
    > > private:
    > > int ii;
    > > double dd;
    > > ABC * abcPtr; // will point to a dynamically created ABC object
    > > };
    > >
    > >
    > > // default ctor
    > > Example2::Example2()
    > > {
    > > abcPtr = new ABC; // dynamically create an ABC object and have
    > > // the class member abcPtr point to it.
    > > ii = 10; // assign some value
    > > dd = 3.765; // assign some value
    > > }
    > >
    > >
    > > // copy ctor
    > > Example2::Example2(const Example2 & src)
    > > :ii(src.ii),dd(src.dd) // MIL
    > > {
    > > abcPtr = new ABC; // Same as default ctor above.
    > > *abcPtr = *(src.abcPtr); // Do a deep copy.
    > > }
    > >
    > >
    > > // dtor
    > > Example2::~Example2()
    > > {
    > > delete abcPtr; //Destroy the dynamically created ABC object.
    > > abcPtr = 0; //Not necessary, but prevents a calamity if an
    > > // attempt is made to destroy the same
    > > // ABC object a second time.
    > > }
    > >
    > > // overloaded assignement operator
    > > Example2 & Example2::eek:perator = (const Example2 & RHS)
    > > {
    > > //First check that the operand on the Right Hand Side (RHS)
    > > // of the assignment operator is not the exact same object
    > > // as the operand on the LHS of the assignment operator.
    > > // We do this by testing to see if both objects have the
    > > // same address!
    > >
    > > if (this == &RHS) // if true, we are done!
    > > {
    > > return *this; // return the original LHS operand
    > > }
    > > else // we still have some work to do
    > > {
    > > ii = RHS.ii; // a shallow copy is good enough here
    > > dd = RHS.dd; // here as well
    > >
    > > *abcPtr = *(RHS.abcPtr); // here we need a deep copy.
    > >
    > > return *this; // return the modified LHS operand.
    > > }
    > > }
    > >
    > >
    > > // Simple program to test our Example2 class
    > > int main()
    > > {
    > > Example2 ex2_1, ex2_2; // Will invoke default ctor (twice).
    > >
    > > ex2_1 = ex2_2; // Will invoke overloaded Assignment operator.
    > >
    > > Example2 ex2_3(ex2_1); // Will invoke copy ctor.
    > >
    > > return 0; // We are finished!
    > >
    > > } // end of main() - Example2 dtor will be invoked 3 times

    >
    > I believe that the code above is a typical example of real code, but
    > anyway it is bad code. A rule of thumb is that code in a class should
    > be centered around the purpose of the class itself ("the class, the
    > whole class and nothing but the class"), and the above code is mainly
    > centered on copying a pointer!
    > My recommendation is that you learn your students (basic) template
    > programming before pointers. This should enable your students to write
    > a safe-pointer template for the class above without obfuscating
    > anything.
    >
    > Kind regards
    > Peter


    Thanks for your suggestion Peter. Can you point me to a C++ text I can
    use for my course that:

    a) covers templates before pointers
    b) teaches the "good code" way of doing what I am trying to teach in my
    code example above

    I have learned the hard way that if the text does not reasonably
    closely follow the course, the typical student will be extemely
    unhappy.

    Thanks in advance,

    Bob

    Thanks
     
    blangela, Nov 26, 2006
    #3
  4. In article <>,
    "blangela" <> wrote:

    > Thanks for your suggestion Peter. Can you point me to a C++ text I can
    > use for my course that:
    >
    > a) covers templates before pointers
    > b) teaches the "good code" way of doing what I am trying to teach in my
    > code example above
    >
    > I have learned the hard way that if the text does not reasonably
    > closely follow the course, the typical student will be extemely
    > unhappy.


    At the risk of answering for Peter,

    Accelerated C++
    Koenig & Moo
    http://www.acceleratedcpp.com/

    This is the most highly regarded introductory C++ book available today.
    I have a copy myself. I regret that I have not read all of it, but I
    have skimmed it. And I deeply regret that this book was not available
    when I was learning C++.

    From the online table of contents:

    http://www.acceleratedcpp.com/details/contents.html

    > Chapter 8 Writing generic functions

    ....
    > Chapter 10 Managing memory and low-level data structures
    > 10.1 Pointers and arrays


    One of the really nice things about this book is that it teaches good
    style right from the start. And it starts with high level stuff, not
    low level stuff. The student learns to write amazingly useful programs
    from day 1 using the std::lib. Defining one's own types and managing
    memory are covered later.

    If I were teaching an introductory course on C++, I would not even think
    of using anything else.

    No, I don't have a financial interest in this book. I wish I did
    though. :)

    -Howard
    Committee member WG21 (C++ Programming Language)
    Library Working Group Chairman
     
    Howard Hinnant, Nov 26, 2006
    #4
  5. blangela

    peter koch Guest

    blangela skrev:
    > peter koch wrote:

    [snip]
    > >
    > > I believe that the code above is a typical example of real code, but
    > > anyway it is bad code. A rule of thumb is that code in a class should
    > > be centered around the purpose of the class itself ("the class, the
    > > whole class and nothing but the class"), and the above code is mainly
    > > centered on copying a pointer!
    > > My recommendation is that you learn your students (basic) template
    > > programming before pointers. This should enable your students to write
    > > a safe-pointer template for the class above without obfuscating
    > > anything.
    > >
    > > Kind regards
    > > Peter

    >
    > Thanks for your suggestion Peter. Can you point me to a C++ text I can
    > use for my course that:
    >
    > a) covers templates before pointers
    > b) teaches the "good code" way of doing what I am trying to teach in my
    > code example above
    >
    > I have learned the hard way that if the text does not reasonably
    > closely follow the course, the typical student will be extemely
    > unhappy.
    >
    > Thanks in advance,
    >
    > Bob
    >

    Hi Bob

    I am happy that Hinnant could answer for me, as I haven't had the
    chance to read any suitable beginners book. I learned C++ partially
    from Stroustrups books and luckily from some collegues who already knew
    how to C++ (no - far from every C++ programmer knows that!).
    All sources speak very highly about Accelerated C++ so jump ahead and
    buy the book.
    Good luck with your teaching.

    /Peter
     
    peter koch, Nov 26, 2006
    #5
  6. blangela

    blangela Guest

    Howard Hinnant wrote:
    > In article <>,
    > "blangela" <> wrote:
    >
    > > Thanks for your suggestion Peter. Can you point me to a C++ text I can
    > > use for my course that:
    > >
    > > a) covers templates before pointers
    > > b) teaches the "good code" way of doing what I am trying to teach in my
    > > code example above
    > >
    > > I have learned the hard way that if the text does not reasonably
    > > closely follow the course, the typical student will be extemely
    > > unhappy.

    >
    > At the risk of answering for Peter,
    >
    > Accelerated C++
    > Koenig & Moo
    > http://www.acceleratedcpp.com/
    >
    > This is the most highly regarded introductory C++ book available today.
    > I have a copy myself. I regret that I have not read all of it, but I
    > have skimmed it. And I deeply regret that this book was not available
    > when I was learning C++.
    >
    > From the online table of contents:
    >
    > http://www.acceleratedcpp.com/details/contents.html
    >
    > > Chapter 8 Writing generic functions

    > ...
    > > Chapter 10 Managing memory and low-level data structures
    > > 10.1 Pointers and arrays

    >
    > One of the really nice things about this book is that it teaches good
    > style right from the start. And it starts with high level stuff, not
    > low level stuff. The student learns to write amazingly useful programs
    > from day 1 using the std::lib. Defining one's own types and managing
    > memory are covered later.
    >
    > If I were teaching an introductory course on C++, I would not even think
    > of using anything else.
    >
    > No, I don't have a financial interest in this book. I wish I did
    > though. :)
    >
    > -Howard
    > Committee member WG21 (C++ Programming Language)
    > Library Working Group Chairman


    Thanks Howard. I will see if my school can get me a desk copy to
    evaluate.

    Bob
     
    blangela, Nov 27, 2006
    #6
  7. blangela

    blangela Guest

    peter koch wrote:
    > blangela skrev:
    > > peter koch wrote:

    > [snip]
    > > >
    > > > I believe that the code above is a typical example of real code, but
    > > > anyway it is bad code. A rule of thumb is that code in a class should
    > > > be centered around the purpose of the class itself ("the class, the
    > > > whole class and nothing but the class"), and the above code is mainly
    > > > centered on copying a pointer!
    > > > My recommendation is that you learn your students (basic) template
    > > > programming before pointers. This should enable your students to write
    > > > a safe-pointer template for the class above without obfuscating
    > > > anything.
    > > >
    > > > Kind regards
    > > > Peter

    > >
    > > Thanks for your suggestion Peter. Can you point me to a C++ text I can
    > > use for my course that:
    > >
    > > a) covers templates before pointers
    > > b) teaches the "good code" way of doing what I am trying to teach in my
    > > code example above
    > >
    > > I have learned the hard way that if the text does not reasonably
    > > closely follow the course, the typical student will be extemely
    > > unhappy.
    > >
    > > Thanks in advance,
    > >
    > > Bob
    > >

    > Hi Bob
    >
    > I am happy that Hinnant could answer for me, as I haven't had the
    > chance to read any suitable beginners book. I learned C++ partially
    > from Stroustrups books and luckily from some collegues who already knew
    > how to C++ (no - far from every C++ programmer knows that!).
    > All sources speak very highly about Accelerated C++ so jump ahead and
    > buy the book.
    > Good luck with your teaching.
    >
    > /Peter


    I had a look at the table of contents and some important topics seem to
    be missing. For example exception handling and operator overloading .
     
    blangela, Nov 27, 2006
    #7
  8. blangela

    Guest

    On Nov 27, 5:32 am, "blangela" <> wrote:
    > peter koch wrote:
    > > I am happy that Hinnant could answer for me, as I haven't had the
    > > chance to read any suitable beginners book. I learned C++ partially
    > > from Stroustrups books and luckily from some collegues who already knew
    > > how to C++ (no - far from every C++ programmer knows that!).
    > > All sources speak very highly about Accelerated C++ so jump ahead and
    > > buy the book.
    > > Good luck with your teaching.

    >
    > > /PeterI had a look at the table of contents and some important topics seem to

    > be missing. For example exception handling and operator overloading .


    I don't own a copy of the book, but I'd suspect that at least
    overloading of the assignment-operator is covered somewhere. And
    chapter 12 "Making class objects act like values", sounds like it might
    include more overloading of operators.

    --
    Erik Wikström
     
    , Nov 27, 2006
    #8
  9. blangela

    peter koch Guest

    blangela skrev:
    > peter koch wrote:
    > > blangela skrev:
    > > > peter koch wrote:

    > > [snip]
    > > > >

    [snip]
    > > I am happy that Hinnant could answer for me, as I haven't had the
    > > chance to read any suitable beginners book. I learned C++ partially
    > > from Stroustrups books and luckily from some collegues who already knew
    > > how to C++ (no - far from every C++ programmer knows that!).
    > > All sources speak very highly about Accelerated C++ so jump ahead and
    > > buy the book.
    > > Good luck with your teaching.
    > >
    > > /Peter

    >
    > I had a look at the table of contents and some important topics seem to
    > be missing. For example exception handling and operator overloading .


    Hi Bob

    I just downloaded the code and it seems that throw is introduced in
    chapter 4 while operator overloading is used in chapter 14. So (of
    course!) those subjects are covered too.


    Kind regards
    Peter
     
    peter koch, Nov 27, 2006
    #9
  10. In article <>,
    "blangela" <> wrote:

    > I had a look at the table of contents and some important topics seem to
    > be missing. For example exception handling and operator overloading .


    Fwiw, exception throwing/catching is introduced in chapter 4, but there
    is no section devoted to the subject. Rather it is simply added to an
    ongoing example. I don't know if the book covers exception safety.
    After glancing at the index my best guess would be no.

    There's a decent treatment of exception safety here:

    http://www.research.att.com/~bs/3rd_safe0.html

    though be forewarned it isn't perfect. There's an exception safety bug
    in vector_base. But the general idea is correct and good.

    The notion and use of operator overloading is introduced very early in
    Accelerated C++, in section 1.2. Examples of how to overload operators
    are in section 11.2 "Defining Abstract Data Types". There is an example
    Vec<T> with an operator[](size_type). I'm not positive if this is the
    first such example in the book or not.

    -Howard
     
    Howard Hinnant, Nov 27, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,926
    Tris Orendorff
    Apr 29, 2004
  2. Alex
    Replies:
    2
    Views:
    1,253
  3. Replies:
    26
    Views:
    2,140
    Roland Pibinger
    Sep 1, 2006
  4. blangela
    Replies:
    13
    Views:
    646
    blangela
    Nov 27, 2006
  5. blangela
    Replies:
    1
    Views:
    491
    Alf P. Steinbach
    Nov 27, 2006
Loading...

Share This Page