Clever C ++

Source: Internet
Author: User

Today, my senior friend talked about the C ++ questions during the recruitment of the company. After listening to these questions, I felt that C ++ was untested. I really need to check out objective C ++. Of course, just share your feelings ....
1. The first is to exchange two data items. We usually use the third variable, so we can know at most how to exchange the real number without the third variable. What is the addition ~~~ But, said the senior brother, which method cannot be used, myGod. Is there a third method ~~~ If I go to the interview, I will die. So what is the method, that is, it can be an exclusive or, efficient, and can process any data type. As follows:

View plaincopy to clipboardprint?
  1. >>># Swap two integer number
  2. >>> A =10
  3. >>> B =23
  4. >>>Print "The A = {0}, B = {1} before swapping". Format (A, B)
  5. The A =10, B =23Before swapping
  6. >>> A ^ = B
  7. >>> B ^ =
  8. >>> A ^ = B
  9. >>>Print "The A = {0}, B = {1} After swapping". Format (A, B)
  10. The A =23, B =10After swapping

2. Global variables are placed in the heap, while local variables are placed in the stack, such as new and static variables that open up different or declarative variables are all placed in the heap. Therefore, a local pointer cannot be returned in a function, as shown in figure

View plaincopy to clipboardprint?
    1. VoidFoo (Char* Pchar)
    2. {
    3. Pchar =New Char[10];
    4. CharTempstr [] ="Hexiong";
    5. Strcpy (pchar, tempstr );
    6. Pchar [strlen (tempstr)] ='\ 0';
    7. }

In this case, an error is reported. Of course, there are two solutions. The first one is to use reference ....

View plaincopy to clipboardprint?
    1. VoidFoo (Char* & Pchar)
    2. {
    3. Pchar =New Char[10];
    4. CharTempstr [] ="Hexiong";
    5. Strcpy (pchar, tempstr );
    6. Pchar [strlen (tempstr)] ='\ 0';
    7. }

Second, use **

View plaincopy to clipboardprint?
    1. VoidFoo (Char** Pchar)
    2. {
    3. * Pchar =New Char[10];
    4. CharTempstr [] ="Hexiong";
    5. Strcpy (* pchar, tempstr );
    6. (* Pchar) [strlen (tempstr)] ='\ 0';
    7. }

Of course, I prefer the first method .....

 

----------------- Create high-qualityArticleFor more information, thank you ---------------

To create high-quality articles, pleaseRecommendationCome on .... Thank you. Please pay attention to my subsequent articles, which will be even better.

Sina Weibo: http://weibo.com/baiyang26

The wine ghost official blog: http://www.ibaiyang.org [recommended to use Google Reader subscription]

Pour wine into the official Douban: http://www.douban.com/people/baiyang26/

If you want to repost this blog,Please specify the source

If you have any comments or suggestions on this article,Leave a message

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.