Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Primer, Fourth Edition

Reply
Thread Tools

C++ Primer, Fourth Edition

 
 
huangshan
Guest
Posts: n/a
 
      10-12-2006
hi all

I read "C++ Primer, Fourth Edition" ,
but can't understand this sentence
"By making the parameters const references, we allow types that do not allow
copying."
in "16.1. Template Definitions" .

who can tell me?
thanks

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      10-12-2006
In article <egka9l$1vt$(E-Mail Removed)99.com>,
"huangshan" <(E-Mail Removed)> wrote:

> hi all
>
> I read "C++ Primer, Fourth Edition" ,
> but can't understand this sentence
> "By making the parameters const references, we allow types that do not allow
> copying."
> in "16.1. Template Definitions" .
>
> who can tell me?


class NonCopyable {
NonCopyable( const NonCopyable& );
NonCopyable& operator=( const NonCopyable& );
public:
NonCopyable();
};

void fn1( NonCopyable nc ); // this will fail to compile
void fn2( const NonCopyable& nc ); // this will compile

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longerハhave anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-12-2006
huangshan wrote:
> hi all
>
> I read "C++ Primer, Fourth Edition" ,
> but can't understand this sentence
> "By making the parameters const references, we allow types that do
> not allow copying."
> in "16.1. Template Definitions" .
>
> who can tell me?


class NoCopies {
NoCopies(NoCopies const&); // copying prohibited
public:
NoCopies(int);
};

void foo(NoCopies);
void bar(const NoCopies&);

int main()
{
NoCopies nc(42);
foo(nc); // error -- must be able to copy
bar(nc); // no error
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-12-2006
* Victor Bazarov:
> huangshan wrote:
>> hi all
>>
>> I read "C++ Primer, Fourth Edition" ,
>> but can't understand this sentence
>> "By making the parameters const references, we allow types that do
>> not allow copying."
>> in "16.1. Template Definitions" .
>>
>> who can tell me?

>
> class NoCopies {
> NoCopies(NoCopies const&); // copying prohibited
> public:
> NoCopies(int);
> };
>
> void foo(NoCopies);
> void bar(const NoCopies&);
>
> int main()
> {
> NoCopies nc(42);
> foo(nc); // error -- must be able to copy
> bar(nc); // no error
> }


Just to fill out the picture, the /current/ standard doesn't allow

bar( NoCopies( 42 ) );

because in the current standard, when binding a temporary to a reference
to const, the compiler is allowed to copy the temporary any number of
times, i.e. a copy constructor must be available.

So the phrasing "allow types that do not allow copying" allows an
interpretation where one believes more is allowed than is actually
allowed, but, types that do not allow copying are allowed as far as the
current standard allows (heh).

As I understand it this will be fixed in C++0x.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT: Happy Fourth Anniversary Frisbee MCSE 48 11-01-2005 09:33 PM
Trying for the third or fourth time... JaneB Firefox 4 05-12-2005 06:56 AM
Happy Fourth of July PuppyKatt Computer Support 8 07-05-2004 03:22 PM
DVD Verdict reviews: BABYLON 5: THE COMPLETE FOURTH SEASON and more! DVD Verdict DVD Video 0 02-20-2004 10:04 AM
Fourth edition of CPPPL Vinodh Kumar P C++ 1 01-22-2004 05:47 AM



Advertisments