Happy: 2nd Chapter Topic Notes

Source: Internet
Author: User

Judging question 1

One of the problems involved is the problem of the value of the assignment constructor in the class. If a copy constructor is allowed to pass a value, the call to the copy constructor is generally recursive within the assignment constructor, and eventually inevitably results in a stack overflow.

If you look closely, you will find that if you call the copy constructor, the input parameters of the assignment constructor are necessarily reference forms.

For example

A (const a &other)

Interview Question 1

When you define a class, there is still a little bit of uncertainty about the constructor.

An operator overload was attempted. It feels okay.

1. Input as reference, output as reference

2. For pointers, pay attention to allocating memory and freeing memory.

//Offer1classcmystring{ Public: CMyString (Char* PData = NULL);//this should only be a statement.CMyString (Constcmystring&str); ~cmystring (void); CMyString&operator=(ConstCMyString &str);Private:    Char*m_pdata;}; CMyString& cmystring::operator= (ConstCMyString &str) {    if( This= = &str)return* This;    Delete[]m_pdata; M_pdata==NULL; M_pdata=New Char[Strlen (Str.m_pdata) +1];        strcpy (M_pdata, str.m_pdata); return* This;}
Interview Question 2

Singleton mode: Single case mode. In mathematics and logic, Singleton is defined as "a set with and only one element"

If a class is associated with a singleton pattern, then there is no doubt that this is a special class, because this class has only one object, so it is called a singleton, a single example. In general, this single case is very easy to access by the outside world.

Advantages: Save system resources.

The method for generating an instance is a static method.

//Offer2 implementing Singleton modeclasssingleton{ Public:    Staticsingleton&getinstance () {StaticSingleton Instance; returnInstance; }Private: Singleton (); Singleton (Constsingleton&Other ); ~Singleton (); Singleton&operator=(Constsingleton&Other );};
Interview Question 3

Note that the int* matrix can only represent one-dimensional arrays. This problem suddenly became so easy when the idea was known.

//Offer3BOOLFind (int* Matrix,intRowsintColumnsintNumber ) {    BOOLIsfound =false; if(Matrix! = NULL && rows >0&& columns >0)    {        introw = rows-1; intColumn =0;  while(Row >=0&& Column <columns) {            if(matrix[row*columns + column] = =Number ) {Isfound=true;  Break; }            Else            {                if(matrix[row*columns + column] <Number ) {                    ++column; }                Else                {                    --Row; }            }        }    }    returnIsfound;}

Question 2:

When two strings are defined as char str[] types, determine whether two strings are equal, and you need to determine if two are pointing to the same memory space. This question is very important. It is not the same as the content. Another: char* str does not have this problem. They are automatically pointed to the corresponding memory address when they are defined.

Happy: 2nd Chapter Topic Notes

Related Article

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.