Академический Документы
Профессиональный Документы
Культура Документы
Subscribe
Newsletters
RSS
Log in
Home Articles News Blogs Source Code Dobbs on DVD Dobbs TV Webinars & Events Go Parallel
private:
int* data;
};
View All Videos
drdobbs.com/184401400;jsessionid=JI… 1/6
01/12/2010 Dr Dobbs - C++ Made Easier: The Rule…
memory twic e, the effec t of which is undefined.
We have seen the claim that the sign of trouble in the IntVec c lass
is that it c ontains a pointer, and that classes that have pointers as
data members should have copy construc tors. We disagree with
this claim, having c ome to realize that the pointer by itself is
harmless. After all, a pointer is just a value. Why should there be
anything intrinsically more harmful about copying a pointer than
about copying any other value?
The true sign of trouble in this class is that the c lass has a
destructor. In almost all cases, when a c lass has a destructor, it
also needs a c opy c onstructor and an assignment operator. To see
that it is the destructor that is fundamental, try removing it. Doing
so c reates a memory leak, of course, but it also avoids the trouble
assoc iated with multiple deletion. If you are willing to tolerate the
memory leak that stems from the lack of a destructor, there is no
additional reason why this class needs a copy construc tor or
assignment operator.
private:
int* data;
This solution to the problem has the great virtue of requiring almost
no thought. As long as no one tries to copy an IntVec, we don’t
need to worry about how to do so. Moreover, any attempts to
c opy an IntVec will be caught during c ompilation.
Of c ourse, we may wish to make our c lass more useful, rather than
merely outlawing operations that we were unwilling to take the
trouble to define. To do so, we must think about what c opying an
IntVec objec t means. There are several possible meanings, the
most straightforward of whic h is probably to say that c opying an
IntVec objec t c opies its contents. In that c ase, we c an define the
c opy c onstructor and assignment operator this way:
drdobbs.com/184401400;jsessionid=JI… 2/6
01/12/2010 Dr Dobbs - C++ Made Easier: The Rule…
private:
int* data;
int size;
};
You c an see that our modifications to this little class have more
than doubled its size. These modific ations all stem from our decision
to define the copy constructor and assignment operator in ways
that are consistent with the destructor.
We can c onc lude, then, that if our class has a destructor, that
destructor does something important to the state of the program
outside the class objec t itself. Now, let’s suppose that our c lass
does not have a c opy constructor. Then copying an object of that
c lass will copy all of its data members. When these two objects are
destroyed, the destructor will run twic e. Moreover, the information
available to the destructor in the objec t being destroyed will be the
same in each case, because eac h data member of the copy will be
a c opy of the c orresponding data member of the original. We
already know that we care whether the destruc tor was exec uted
onc e or not at all. It is therefore highly likely that we also c are
whether the destructor is executed twice, as opposed to onc e. Yet
if we do not have a copy c onstructor, copying an object will cause
a destruc tor that would otherwise have been executed onc e to be
exec uted twic e under the same conditions. Suc h duplic ate
exec utions are a recipe for trouble.
A similar argument lets us conc lude that a c lass with a destruc tor
must also have an assignment operator. If such a class does not
have an explicitly defined assignment operator, assigning one
object of that class to another will assign all the source’s data
members to the c orresponding data members of the destination.
After that assignment, the destination’s data members will be
c opies of the sourc e’s data members, and the destructor will be in
exac tly the same kind of trouble that it would have been in had the
object been copied rather than assigned.
class Thing {
public:
Thing() { /* ... */ }
Thing(const Thing& t):
data(t.data)
{ } // don't copy the cache
Thing& operator=(const Thing& t) {
data = t.data; // copy the data
drdobbs.com/184401400;jsessionid=JI… 3/6
01/12/2010 Dr Dobbs - C++ Made Easier: The Rule…
ephemeral, the c lass will need a destructor. For example, a class
with a c opy c onstructor that alloc ates a resourc e will need a
destructor that deallocates that resource, unless the author is
willing for the class to leave the resources alloc ated.
class String_with_marker {
// ...
private:
std::string str;
int marker;
};
class String_with_marker {
// ...
private:
std::string str;
// changed
std::string::iterator marker;
};
class String_with_marker {
// ...
public:
// added
String_with_marker(const String_with_marker& s):
str(s.str),
marker(str + (s.marker - s.str.begin())) { }
String_with_marker& operator=(const String_with_marker& s)
{
str = s.str;
marker = str + (s.marker - s.str);
return *this;
}
private:
std::string str;
std::string::iterator marker;
};
Conclusion
Designing a copy construc tor for a class might be more trouble than
it is worth. Consider our IntVec class, where adding a copy
c onstructor and assignment operator more than doubled the size of
the c lass.
drdobbs.com/184401400;jsessionid=JI… 4/6
01/12/2010 Dr Dobbs - C++ Made Easier: The Rule…
so, c onsider making them private so that programmers won’t try to
use them by ac cident. In all other cases, you should write an
assignment operator and c opy c onstructor whenever you write a
destructor.
Notes
[2] Classes often have an empty virtual destruc tor, which is there
only bec ause it is virtual and not to do any actual work. Such
destructors don’t count for the purpose of this disc ussion.
Care to Comment?
Subject (max length: 75):
Comment:
Captcha:
drdobbs.com/184401400;jsessionid=JI… 5/6
01/12/2010 Dr Dobbs - C++ Made Easier: The Rule…
CIOs & IT Professionals Software Developers Vertical Markets Global Communications Most Popular
Service Providers
Black Hat Dr. Dobbs Advanced Trading ANTenna
Cloud Connect Dr. Dobbs M-Dev Bank Systems & Technology Heavy Reading Bob Evans' Global CIO
Dark Reading Dr. Dobbs Digest InformationWeek Government Heavy Reading Insiders Cable Catchup
Enterprise 2.0 Dr. Dobb's Update InformationWeek Healthcare Pyramid Research David Berlind's Tech Radar
Enterprise Connect TechWeb.com Insurance & Technology Light Reading Digital Life
Enterprise Efficiency Light Reading / Telecom Light Reading Mobile Evil Bytes
Web & Digital Professionals
HDI Wall Street & Technology Light Reading Cable InformationWeek Analytics
Internet Evolution Light Reading Europe
InformationWeek My Interop
Web 2.0 Expo Game Industry Professionals
InformationWeek 500 Light Reading Asia Jon Erickson's Blog
Web 2.0 Summit Gamasutra Ethernet Expo
InformationWeek 500 Conference Microsoft/Window s Blog
TechWeb.com Game Developers Conference (GDC) TelcoTV
InformationWeek Analytics Monkey Bidness
Independent Games Festival Tow er Summit
InformationWeek Events Government Officials Over the Air
Game Developer Magazine Light Reading Live & Virtual Events
InformationWeek Global CIO Gov 2.0 Expo The Philter
GDC Europe Webinars
InformationWeek Healthcare Gov 2.0 Summit Valley Wonk
GDC China
InformationWeek India GTEC Wolfe's Den
Game Career Guide
InformationWeek SMB InformationWeek Government
Game Advertising Online
Intelligent Enterprise TechWeb.com
Interop
Netw ork Computing
No Jitter
Plug into the Cloud
TechWeb.com
Reprints TechWeb Digital Library / White Papers TechWeb Events Calendar TechWeb.com
Terms of Service | Privacy Statement | Copyright © 2010 UBM TechWeb, All rights reserved.
drdobbs.com/184401400;jsessionid=JI… 6/6