Deep versus Shallow Copy - Part 2

B

blangela

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
 
P

peter koch

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
 
B

blangela

peter said:
blangela skrev:

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
 
H

Howard Hinnant

"blangela said:
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
 
P

peter koch

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
 
B

blangela

Howard said:
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


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
 
B

blangela

peter said:
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 .
 
E

eriwik

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.
 
P

peter koch

blangela skrev:
peter said:
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
 
H

Howard Hinnant

"blangela said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top