C + + interview

Source: Internet
Author: User
Tags modifier

Bjarne Stroustrup 's C + + style and technical FAQ (Chinese version)

1, as far as possible to say the role of the static keyword?

(1) static modifier function local variable (including the main function), the variable will not be destroyed as the function scope exits, but only once the memory, the next call is the value of the last call.

(2) static modifier global variable, limit the variable can only be accessed by this file, cannot be accessed by other files.

(3) Static modifier functions can only be accessed by this file and cannot be accessed by other files.

(4) static menber belongs to the class, and all instance objects of that class share a copy (here is a noun)

(5) Static menber function does not accept the this pointer and can only access members

The problem arises from this: the use of static variables and static member functions?

2. Say the usage of const as much as possible?

Const can not simply think of as "constant", C language to use constants, such as PI, will use define,<<effective c++>> recommended const, because the use of macros if compiled error, will not show Pi, Only 3.1415 digits are indicated. The real purpose of the const is to remind the programmer that the object is non-modifiable .

(1) Modifying a variable with a const, initializing it, forgetting to initialize it compiles an error

(2) When const and the Pointer Act, the <<c++ primer 5th>> is divided into pointer to const (pointer to the constant, the pointer is pointing to a constant, the object cannot be modified with a pointer, and the object is not required to be const). Const pointer (pointers cannot point to other objects), or const int * Const P =&a;

(3) const-Modified formal parameter, which indicates that an argument cannot be modified by a formal parameter and can receive a const object as an argument or Non-const object

(4) const Reference, cannot use reference to modify the quoted object, but the object itself can be modifiable, the reference itself cannot point to other objects

(5) Const modifier member function, the function cannot modify the member variable, but if the variable is a pointer, the pointer can be used to modify the referred resource (see Effctive C + + item); Const men fun can be overloaded, such as the const T &operato R[] () const, t&operator[] (), the former can be called by a const T type object, which cannot, because the const object converts the this pointer to a const pointer

(6) The const modifier scope type, so that there is no a*b=c (assuming overloaded *) Such a strange legal code.

1 Const operator* (const classa& A1,const classa& A2);   2  The return result of operator * must be a const object. If not, such a perverted code will not compile an error  :3ClassA A, B, C  ; 4 //

(7) The advantages of const and define

Ps:const Mem Fun implies that there is a const *this parameter, and the static mem fun implies that there is no this pointer, so it is not possible to modify the member function by compiling the

3. What is the difference between a reference and a pointer? Why not string *const p instead of string &p

First, why introduce references?

Because a reference is added to support operator overloading. Here is a hypothesis, if there is no reference, then use the pointer to operator the overloading operation. A operator + (const a *a, const a *_a); Then use the time, &a + &b, so it seems to be not very uncomfortable. Use pointers with poor readability, *p.

The introduction of the concept of reference can not only satisfy the overload operator, but also the flexibility of overloading value and pointer. And the reference also brings an attribute that cannot be overridden by a pointer: a reference to a temporary object. Because the reference must be assigned at the time of definition, it cannot be changed later. String &s=string ();

(1) References must be initialized, no void references, compile-time references must bind to an object, references are not objects, only aliases, references themselves do not allocate memory.

(2) cannot have string & const S, can have const string&s, the latter is a constant reference.

(3) The dereference operation is to the pointer, gets the object that the pointer refers to, not the reference operation

(4) sizeof (reference) Gets the size of the bind object, and sizeof (pointer) Gets the size of the pointer itself

(5) As a function parameter, if the argument is a pointer, the initial taxiing parameter with the argument is a pass-through (which is also a value in nature)--may require a pointer type conversion, or a derived class pointer to the base class pointer, if it is a reference, the object itself is passed, no additional replication overhead is required. If you need to modify the pointer, you can use a level two pointer

4. What are the characteristics of "reference" as a function parameter?

(1) The effect of passing a reference to a function is the same as passing a pointer. At this point, the parameter of the function is used as the real parametric or an alias of the object in the original Melody function, so the operation of the parameter variable in the modulated function is the operation of the corresponding target object (in the keynote function).

(2) using the parameters of the reference transfer function, in memory does not produce a copy of the argument, it is directly to the actual parameter operation, while using the general variable transfer function parameters, when a function call, you need to assign a storage unit to the parameter, the parameter variable is the copy of the argument variable; Therefore, when the parameter passes the data is large, uses the reference to pass the parameter with the general variable efficiency and occupies the space to be good.

(3) The use of pointers as parameters of the function, although also can achieve with the use of reference, but in the function of the parameter allocation of the same storage unit, and the need to re-use "* pointer variable name" in the form of operations, which is prone to error and poor program reading; On the other hand, at the call point of the keynote function You must use the address of the variable as the argument. And references are easier to use and clearer.

See some C + + face, there are some basic problems such as the role of const, virtual function of the implementation mechanism, familiar with shared_ptr, these are the language features of C + +, if you want to examine the STL, I currently only until the vector, list, the various algorithms other containers are not familiar with, Not to mention the time efficiency and the space cost of the container, if you want to examine the algorithm, the list and the two-fork tree, the quick sort, the heap sorts, this can combine Make_head () and other heap functions, if it is to investigate the code implementation, such as memcpy, Strstr, strcmp, and so on. So I need to firmly grasp the basic knowledge of C + +, STL, and then brush questions, master a variety of frequent test algorithms, these are the advantages I can achieve, Linux, projects, etc. is not my short-term ability to be good at the place.

C + + interview

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.