Article Discussion
The Law of The Big Two
Summary: Welcome to the first installment of Smart Pointers, a monthly- ish column written exclusively for The C++ Source. Here, two seasoned [1] programmers—Bjorn Karlsson and Matthew Wilson—carefully dissect C++ idioms, tricks, and power techniques. To make up for the fact that these are very serious topics, we shall occasionally expose you to really crummy programming jokes, too. Now, who said there was no such thing as a free lunch? In this instalment the authors update The Law of The Big Three, and explain which of the three magic member functions is often not needed.
37 posts on 3 pages.      
« Previous 1 2 3 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: May 9, 2008 9:32 AM by m
Chuck
Posts: 32 / Nickname: cda / Registered: February 11, 2003 0:06 PM
Re: I'm blown away
October 7, 2004 7:55 PM      
> Providing optional GC would be a big win. Providing
> useful (and standard) counted and counting_ptr pairs
> would fix a ton of systems, but we are still fooling
> around with this?
>
> Color me disgusted.

Well, an attractive collection of smart pointers has been added to C++0x that would make you quite happy, and GC is under discussion. (Color yourself updated.) But in the meantime, we need to get by.
Steve
Posts: 8 / Nickname: essennell / Registered: September 7, 2004 10:19 PM
Re: I'm blown away
October 7, 2004 10:38 PM      
> that this is still the state of discourse in C++.
>
> Frozen in time for something like 10 years.

Well folks have been working hard in that time for a "new release" of the standard, which seems likely to contain a decent - and useful - collection of pointer wrappers, including counting ones.

If the language had been moving at the rate it was pre 1997, would you have preferred it?

> Still no
> standard reference counting pointer implementation?
> Auto_ptr is weird and error prone and this level of
> f fiddling is just madness.

Ah well, auto_ptr has its uses though. Less wierd I reckon than strtok. And less error prone ;-)

> Providing optional GC would be a big win.

I'm still bemused by the amount of people who think automatic GC is some kind of silver bullet (I'm not suggesting you are one of them). I write in C# and C++ a lot, and I prefer C++'s deterministic destruction. For one thing, it makes exceptions much easier to think about.

Steve
Bronek
Posts: 1 / Nickname: brok / Registered: October 18, 2004 9:37 PM
Re: The Law of The Big Two
October 19, 2004 2:03 AM      
There's one misleading sentence in the article:

Another way of prohibiting copy construction and copy assignment is to make one or more members a reference or const (or const reference, for the especially cautious)—this effectively shuts down the compiler's ability to generate these special member functions

As far as I remember, const or ref members won't disable copy constructor implicitly generated by compiler. It will only disable implicit copy assignment operator, or (more precisely) render program ill-formed when implicitly generated by compiler copy assignment operator is used.
alex
Posts: 1 / Nickname: ipodhelp / Registered: January 24, 2005 8:40 AM
Re: The Law of The Big Two
January 25, 2005 1:51 PM      
can you help me by doing this referal for getting a free ipod or pc you chose..... Just sign in and complet the recomended Call wave form and i will guide you so no personal info is given..at the end(with just referring some people) of the we will both have and ipod and its not a fake..please !http://www.freeiPods.com/?r=14135080

http://www.FreeDesktopPC.com/?r=14319525

http://www.FreeFlatScreens.com/?r=14321387

Thanks
sapa
Posts: 1 / Nickname: sapa / Registered: February 17, 2005 2:27 AM
Re: The Law of The Big Two
February 17, 2005 7:39 AM      
I realize this is a newbie question, but does this code cause a leak for the old value of p_?

Example& operator=(const Example& other) {
// Self assignment?
if (this==&other)
return *this;

*p_=*other.p_; // Uses SomeResource::operator=
return *this;
}
Marsha
Posts: 1 / Nickname: mkpros / Registered: November 17, 2007 9:31 AM
Re: The Rule of The Big Two
November 17, 2007 3:36 PM      
Iam loooking for a Dan Teske from Cleveland Ohio .1969 Grad. of Rhodes High School / St. Thomas More 1965. Please advise if you r him ...Marsha PROS Gorman
m
Posts: 3 / Nickname: mlimber / Registered: November 2, 2006 6:37 AM
Re: The Law of The Big Two
May 9, 2008 9:32 AM      

I realize this is a newbie question, but does this code cause a leak for the old value of p_?

Example& operator=(const Example& other) {
// Self assignment?
if (this==&other)
return *this;

*p_=*other.p_; // Uses SomeResource::operator=
return *this;
}


With all the indirection going on, it's a bit tricky to see, but there's no leak here. The reason is that there's no memory allocation or deallocation, only copying of values. That is, the assignment statement ("*p_=*other.p_;") is copying the value of other's p_ into the location currently pointed to by this's p_. Their allocation remains the same.

A simplified example:


int *i = new int(42);
int *i2 = new int(2008);

*i = *i2; // no leak here; just copying values

delete i2;
delete i;
37 posts on 3 pages.
« Previous 1 2 3 Next »