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

C++ Primer, Fourth Edition

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

Oct 12 '06 #1
3 2509
In article <eg**********@news.cn99.com>,
"huangshan" <hu**************@hotmail.comwrote:
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$B%O(Bhave anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 12 '06 #2
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
Oct 12 '06 #3
* 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?
Oct 12 '06 #4

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

Similar topics

1
by: Vinodh Kumar P | last post by:
Is there any news of CPPPL fourth edition?
1
by: Charles L | last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd Edition'? Charles Leng
5
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
1
by: Ciko | last post by:
I had a look at this book: "C Primer Plus, Fifth Edition Stephen Prata " and I found simply fantastic. My english is little but I had no problem to understand it. Was wondering if you...
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
2
by: subramanian100in | last post by:
In C++ Primer(4th Edition) by Stanley Lippman, in page 162, the following is mentioned: The general syntactic form of a compound assignment operator is a op= b where op= may be one of the...
1
by: mmdspam | last post by:
Hi, Can someone please tell me the answer to exercise 1.20 in C++ Primer 4th edition. I am stuck on this question. Thank you in advance
2
by: jerry | last post by:
can anyone tell me where can i get the keys to c++ primer 4th edition, pdf or chm type are both ok. thank you!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.