473,386 Members | 1,752 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,386 software developers and data experts.

copy constructor and destructor

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

Aug 18 '05 #1
3 1417

Tony Johansson wrote:
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.

Aug 18 '05 #2
Tony Johansson wrote:
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
Aug 18 '05 #3
Tony Johansson wrote:
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.

Aug 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Apricot | last post by:
#include <iostream> #include <string> #include <map> using namespace std ; class tst { public : tst() { cout << "tst::constructor" << endl ; } tst(const tst & that) { cout << "tst::copy...
4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
14
by: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ....then a bitwise object is created w/o calling...
3
by: Matt Bitten | last post by:
Hi, all. I have the same old problem about templates and copy constructors. I know this has been addressed hundreds of times, but despite perusing many old postings, and The Standard as well, I'm...
3
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
3
by: lhr_cool_guy | last post by:
The following code is compiling correctly on Visual Studio .NET 2003 but crashes. The problem is that the copy constructor is not being called when it should be. Am I doing something wrong or is...
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
7
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I...
20
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.