473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Copy Constructors

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
12 1484
"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

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

"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

"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

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

"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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.