By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

Copy Constructors

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Bryan Bullard" <th********************@sbcglobal.net> wrote...
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.
Jul 22 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ca7Wb.212414$Rc4.1722383@attbi_s54...
BTW, what C++ book are you reading that doesn't explain copy constructors?


tc++pl. i'm looking for a different perspective or explanation.
Jul 22 '05 #3

P: n/a
Bryan Bullard wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ca7Wb.212414$Rc4.1722383@attbi_s54...
BTW, what C++ book are you reading that doesn't explain copy constructors?


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ca7Wb.212414$Rc4.1722383@attbi_s54...
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.

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.
Jul 22 '05 #5

P: n/a

"Bryan Bullard" <th********************@sbcglobal.net> wrote in message
news:%H****************@newssvr23.news.prodigy.com ...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ca7Wb.212414$Rc4.1722383@attbi_s54...
BTW, what C++ book are you reading that doesn't explain copy
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
Jul 22 '05 #6

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...

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.


Jul 22 '05 #7

P: n/a
"Bryan Bullard" <th********************@sbcglobal.net> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ca7Wb.212414$Rc4.1722383@attbi_s54...
BTW, what C++ book are you reading that doesn't explain copy
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?
Jul 22 '05 #8

P: n/a
"Bryan Bullard" <th********************@sbcglobal.net> wrote...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...

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.


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
Jul 22 '05 #9

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:818Wb.271856$xy6.1382009@attbi_s02...
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.
Jul 22 '05 #10

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:if8Wb.141063$U%5.646063@attbi_s03...
Did you read about "rule of three" as I recommended?


yes. that was helpful, thanks.
Jul 22 '05 #11

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:818Wb.271856$xy6.1382009@attbi_s02...
"Bryan Bullard" <th********************@sbcglobal.net> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ca7Wb.212414$Rc4.1722383@attbi_s54...
BTW, what C++ book are you reading that doesn't explain copy

constructors?

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


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...)
Jul 22 '05 #12

P: n/a

"Bryan Bullard" <th********************@sbcglobal.net> wrote in message
news:8S****************@newssvr23.news.prodigy.com ...

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.
Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.