Sessions about object life history

Source: Internet
Author: User
Tags constructor sessions

(Translator Note: I read the article from the Internet, very like the author's style of writing, so I use the spare time to translate it and posted on the Internet, I hope you can benefit from it, because I did not contact the author or the copyright owner of this article to obtain the copyright of this article, Therefore, the copyright of this translation should not be owned by me, and I have not gained any benefit from it! Moreover, my admiration for the original author and his copyright owner is like the water of the Tao, so I have never violated any intention of the original author. Of course, to say "interest" or some, I deepened the following C + + knowledge: can only be bound to a constant, nonvolatile reference, the left value does not have such a limit; the reference returned by the function is the left value. And anyway, if a temporary object is referenced in the constructor of an object, and the object dies at the end of the constructor construct, the object becomes a "wild reference". If I understand the mistake, please let me know, thank you, kesongemini@vip.163.com. There are Herb Sutter, "more C + + exceptional" Chinese version has been published by Huazhong University of Science and Technology Press, younger brother secretly pleased to buy a book, Shenyang Miyoshi Street Pine Tech Bookstore into the new book speed is amazing, are 80 percent, do not know if there is no cheaper places:)

A few days before the holidays, I didn't feel the pressure of a deadline (deadline)--The project I was working on was done as planned.

I chose my favorite pastime, and I skimmed the source repository: The source code think-tank? Do not understand, is an analysis of the source software? )。 I often study other people's code, learn some of their skills (new to me), and learn how to avoid errors in their code.

class T
{
public:
T & ref() { return *this; }
};
void f( T & );
int main()
{
f( T().ref() );
}

At first I didn't understand the point of the ref () function, so I removed the call to it-I think the code below should work:int main()
{
 f( T() );
}

However, when I compile, the compiler prompts for an error: binds the very reference to a temporary object. I tapped my forehead--of course, this binding is not allowed. Recall that the first time I met this kind of mistake, the Guru (translator: translated below) gave me an explanation.

"One reason to ban this binding is to prevent tricky bugs, my child, and look at the situation below," she said.class U
{
  // ... 任 何代码 ...
};
void takesAndModifiesU( U & u )
{
  // 执 行操作以修改u的状态
}
class V
{
public:
operator U();
};
void g()
{
V v;
// ...
takesAndModifiesU( v );
// ...
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.