[C ++] polymorphic class face questions

Polymorphic class face questions

  1. What is polymorphism?
    A: Different subjects completed the same event (with the same function), yield different results (internal function to achieve different).
  2. What is overloading, rewrite, re-defined?
Scope Required function name, parameters, return value Other requirements
Overload Two functions in the same scope Function name, the same parameters
Rewrite (cover) Both functions are in the scope of the base and derived classes Function name, parameters, return values ​​are the same Two functions must be virtual
Redefine (hidden) Both functions are in the scope of the base and derived classes The same function name The same name of the base class and derived classes do not form rewritable is redefined
  1. The principle of polymorphism?
    A: With a virtual base table implementation.

  2. The constructor can be virtual function?
    A: No. Because the object virtual function table pointer in the constructor initializer initialization phase only.

  3. The destructor can be virtual function?
    A: Yes. However, in a particular kind of condition it must be defined as a virtual destructor function. (See link).

  4. Can virtual functions defined as static type function?
    A: No. Addresses virtual functions to be saved to the virtual table to go, static objects can not call, so there is no object, there is no virtual table pointer, you can not find the virtual table. No virtual table is certainly not achieve polymorphism. No object, this is no pointer, the virtual function is to achieve a multi-state, defined as static static virtual table function can not find, can not achieve polymorphism.
    Inline function is not correct virtual function

  5. Virtual function can be defined as an inline function?
    A: No. Inline functions not address every time you use an inline function, are carried out in the expansion of local calling, there is an ordinary function address, when calling assembler code to address call (meaning the call) about the general function, but the inline function is no address, was launched at local call. Not the address on the virtual function table

  6. What stage is the virtual function table generated, there is in what?
    A: The virtual function represents the compilation phase is generated. The presence of the code (constant region) of general

  7. Virtual function table (virtual table), the virtual table pointer, virtual functions exist which are of?
    A: virtual function (virtual function modified) and normal functions are present in the code segment, the virtual object is present in the table pointer (pointing virtual table), proven virtual table is present in the code segment (constant regions).

  8. Virtual function table (virtual table), the relationship between the virtual table pointer, virtual function?
    A: virtual table pointer is present in the subject, virtual table pointer to the virtual table, ie a virtual table pointer is the address of the first virtual table. Virtual function pointer table is an array of class virtual table addresses are stored in the virtual function. When the multi-state, through the virtual pointer table, find the virtual table, find virtual function table by the virtual address, a virtual function call.

  9. Object Access virtual function or normal function faster and faster?
    A: 1. If an ordinary objects as fast. Because the generic object calls, an ordinary call, only with the type of;
    2. If the object is a pointer or reference to the object, the normal function block called because polymorphic configuration, run-time virtual function call needs to go to the virtual function table lookup.

  10. What is C ++ diamond inheritance? ? Virtual inheritance principles
    call this inheritance to inherit a multi-sub-class has two or more direct parent when: A: multiple inheritance. Diamond inheritance is a case of multiple inheritance. Two or more subclass of the direct parent of the same and also that the direct parent. This is the diamond inheritance
    diamond virtual inheritance is solved data redundancy and ambiguity of principle. Virtual inheritance through the virtual base table pointer

  11. What is an abstract class? The role of abstract class?
    A: An abstract class is a class containing pure virtual function. The role of abstract class is: forced the derived class to override virtual functions, in addition reflects the interface inheritance (see link) https://blog.csdn.net/weixin_43939593/article/details/103687764

发布了55 篇原创文章 · 获赞 12 · 访问量 5198

Guess you like

Origin blog.csdn.net/weixin_43939593/article/details/103739994