copy constructor and destructor

T

Tony Johansson

Hello!!

Is it correct to say that THE ONLY TIME it's absolute necessary to define a
copy constructor and a destructor when implementing a class is when
allocating dynamic memory

//Tony
 
A

Alipha

Tony said:
Hello!!

Is it correct to say that THE ONLY TIME it's absolute necessary to define a
copy constructor and a destructor when implementing a class is when
allocating dynamic memory

//Tony

no. you write a copy constructor, copy assignment operator (don't
forget the assignment operator), and destructor whenever you obtain any
resources that need to be freed, be it dynamic memory, file handles,
socket handles, create a window, etc.
 
V

Victor Bazarov

Tony said:
Is it correct to say that THE ONLY TIME it's absolute necessary to define a
copy constructor and a destructor when implementing a class is when
allocating dynamic memory

No, that's incorrect. Reference-counted classes need copy-constructor
and destructor implemented because the compiler doesn't know how to
count reference. And that's just one example. I am certain there are
others.

V
 
J

Jaspreet

Tony said:
Hello!!

Is it correct to say that THE ONLY TIME it's absolute necessary to define a
copy constructor and a destructor when implementing a class is when
allocating dynamic memory

//Tony

Not sure why a mention of 'copy constructor', it should have been
'constructor' only. You need a constructor and/or a destructor when you
want to do something more than what the system provides you with. For
example, open a file, etc.

You need a copy constructor when you have to create a copy of an
existing object. There is one provided by the system but you would need
your implementaion of the copy constructor if you have a pointer or an
array as your data member of the class.
 

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,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top