Copy Constructors

B

Bryan Bullard

i have a class that contains a private pointer to a system resource. i'm
quite sure that i will need to have a user defined copy-constructor to do a
deep copy. can someone put me in the right direction (that is, simplified
material or examples)?

-bryan

ps: the private variable that needs attention points to a critical section
semaphore (used to synch access to the object) that is dynamically
allocated/deallocated in ctor/dtor respectively. i believe that a mere
shallow copy will result in deadlock.
 
V

Victor Bazarov

Bryan Bullard said:
i have a class that contains a private pointer to a system resource. i'm
quite sure that i will need to have a user defined copy-constructor to do a
deep copy. can someone put me in the right direction (that is, simplified
material or examples)?

Google for <<"the rule of three" C++>>.

BTW, what C++ book are you reading that doesn't explain copy constructors?
-bryan

ps: the private variable that needs attention points to a critical section
semaphore (used to synch access to the object) that is dynamically
allocated/deallocated in ctor/dtor respectively. i believe that a mere
shallow copy will result in deadlock.

Deadlocks, semaphores, synch access, critical sections, are all OT here.
You might want to post to comp.programming.threads for more information.
 
K

Karl Heinz Buchegger

Bryan said:
tc++pl. i'm looking for a different perspective or explanation.

What is your specific problem in understanding. There is nothing mystical
about a copy constructor. It's just a constructor which takes a reference
to an object of the same type as an argument. Just like all constructors
with arguments it means: initialize the new object by using the passed
argument to do so. Whatever initialization in your specific case may mean.
 
B

Bryan Bullard

Deadlocks, semaphores, synch access, critical sections, are all OT here.
You might want to post to comp.programming.threads for more information.
right, the system resource is really irrelevant to the subject of my post.
i included the post-script for further illustration.

my apologies to the group. please ignore the postscript.
 
J

John Harrison

Bryan Bullard said:
constructors?

tc++pl. i'm looking for a different perspective or explanation.

It may be worth wrapping the pointer a semaphore in its own class (which
will itself contain the pointer to the semaphore). This way the wrapper
class can do the deep copy semantics in its own copy constructor, and your
main class won't have to worry about a thing.

john
 
B

Bryan Bullard

Karl Heinz Buchegger said:
What is your specific problem in understanding. There is nothing mystical
about a copy constructor. It's just a constructor which takes a reference
to an object of the same type as an argument. Just like all constructors
with arguments it means: initialize the new object by using the passed
argument to do so. Whatever initialization in your specific case may mean.
is it true that when the copy constructor is used, no other constructor is
called?

also, i'm a little confused about what should go in the copy constructor and
the overridden assignment operator.
 
V

Victor Bazarov

Bryan Bullard said:
constructors?

tc++pl. i'm looking for a different perspective or explanation.

Perspective? Find a book from a different author, I guess. I
recommend "Effective" series by Scott Meyers. Not that it's so
different, just the goal of the book is orthogonal to TC++PL, I
suppose.

Since we're on the subject, why are you looking for "a different
perspective"? And why don't you like the explanation Dr. Stroustrup
gives? Any particular passage in the book you find incomplete or
difficult to understand?
 
B

Bryan Bullard

Since we're on the subject, why are you looking for "a different
perspective"? And why don't you like the explanation Dr. Stroustrup
gives? Any particular passage in the book you find incomplete or
difficult to understand?
nothing personal against stroustrup (he's always been helpful). however,
there are areas in the book that i feel are possibly too brief in
explaination. sorry that i cannot quote for you. i don't have the text
with me.
 
V

Victor Bazarov

Bryan Bullard said:
is it true that when the copy constructor is used, no other constructor is
called?

also, i'm a little confused about what should go in the copy constructor and
the overridden assignment operator.

A compiler-generated copy constructor invokes copy construction semantics
for all base classes and members (that's to the question about "no other
constructor"). So far there is no mechanism in the language to invoke
other constructors for the same object.

Assignment operator is not involved during construction. It's called for
assignment expressions. The difference between the two is not dramatic,
but still significant, I believe. When construction is happening, the
object is fresh, new, nothing has been previously constructed or used.
When assignment is happening, the object has its previous state and needs
to account for that (dispose of the "old" assets before acquiring "new"
ones, so to speak). Did you read about "rule of three" as I recommended?

V
 
J

jeffc

Victor Bazarov said:
Perspective? Find a book from a different author, I guess.

Or come here and ask. (not sure why needing a different perspective is so
hard to understand either...)
 
J

jeffc

Bryan Bullard said:
right, the system resource is really irrelevant to the subject of my post.
i included the post-script for further illustration.

my apologies to the group. please ignore the postscript.

Don't worry about the grouches around here.
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top