Sword refers to the basic knowledge of offer--c++ interview __c++

Source: Internet
Author: User

The interviewer's basic knowledge of C + + the first type of interview is a direct oral inquiry to understand the concept of C + +, the interviewer is particularly interested in understanding the candidate's understanding of the keyword. For example, in C + +, what 4 keywords are associated with a type conversion. What are the characteristics of these keywords, and should be used in any situation.
The size of this type of topic is a concept that is often asked. For example:
Defines an empty type with no member variables and member functions. The size of the type is obtained, and the result is how much.
The answer is 1, because no information is included in an instance of an empty type, and sizeof should be 0, but when we declare an instance of that type, it must occupy a certain amount of space in memory, otherwise it cannot be used, and the compiler decides how much memory to occupy. An instance of each empty type in VS occupies 1 bytes of space.
If you add a constructor and destructor to the type, then ask sizeof for that type, how much is it?
It's still 1. Calls to constructors and destructors only need to know the address of the function, and the addresses of these functions are only related to the type, not the instance of the type, and the compiler does not add any additional information to the instance because of the two functions.
What if the destructor is marked as a virtual function?
Once the C + + compiler discovers that a virtual function is in a type, it generates a virtual function table for that type and adds a pointer to the virtual function table in each instance of the type. On a 32-bit machine, a pointer for 4 bytes of space, so sizeof to get 4, if the 64-bit machine, a pointer for 8 bytes of space, so sizeof is 8.

Click to link to summary of usage for sizeof keywords


The second type of interview for C + + is to take out the prepared code and let the interviewer analyze the results of the code. For example, the interviewer gives the following code, analyzes the results and gives 3 options: A. Compile error; B. Compilation succeeded, run-time program crashes; C. Compile is running normally, output 10.

[CPP]  View plain  copy  print? #include  <iostream>   using namespace std;      class a    {   private:       int value;   public:        a (int n) { value = n; }        a (A other) { value = other.value; }       //A (const  a& other) { value = other.value; }              void print () { cout << value << endl; }   };      int main ()    {       A a  = 10;       A b = a;       b. Print ();      &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBsp;0;  }   in the preceding code, the parameter passed in the copy constructor A (a) is an instance of a. Because it is a pass-value parameter, we copy the parameters to the actual participant call the copy constructor. Therefore, if a copy constructor is allowed to pass a value, the copy constructor is called within the copy constructor, and a never-ending recursive call is formed, resulting in a stack overflow. Therefore, the standard of C + + does not allow the copying of constructor value parameters, to solve this problem, we can modify the constructor to a (const a& other), that is, to change the value parameter to a constant reference.


The third type is to ask the candidate to write code that defines a type or member function in the implementation type. Many of the code questions that examine C + + syntax revolve around constructors, destructors, and operator overloads. For example, face Test 1 "assignment operator function" is an example.

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.