Location>code7788 >text

C++ Interview Questions 2

Popularity:146 ℃/2024-09-19 11:22:26

8. What's new in C++11?

Automatic type derivation auto, smart pointers (share_ptr, unique_ptr, etc.), for loop simplification, thread-related (std::thread/std::mutex), null pointer nullptr, lambda expressions, and more!

9. is share_ptr thread-safe?

share_ptr contains reference count and data pointer, reference count is atomic operation, thread-safe, but change the data pointer pointing to cause the reference technology plus or minus, not thread-safe. Imagine when a share_ptr reference count is 1, then two threads at the same time to assign it, change the data pointer, it will cause the reference count reduced 2 times, causing crash.

10. What are the methods of inter-thread synchronization

The main ones are lock/mutex, condition variable and future.

  • std::unique_lock<std::mutex> or std::lock_guard<std::mutex> ensures that only one thread at a time accesses the critical zone
  • std::condition_variable can block a thread via the wait method, and another thread can be notified via the Notify method
  • std::future used in conjunction with std::async, you can asynchronously open a thread and store the returned result, another thread through the wait or get method of future can block the thread until the result is returned.

Reference.

  1. C++11 Simple Use of the Thread Synchronization Interfaces std::condition_variable and std::future

11. Mechanisms for implementing polymorphism in virtual functions

Virtual function polymorphism mainly refers to the fact that a base class pointer (reference) calls a virtual function, and if the base class pointer (reference) points to a derived class, it automatically calls a virtual function of the derived class. The mechanism of its realization is through thevirtual function table (math.)cap (a poem)virtual table pointer. When compiling a class containing a virtual function, the corresponding virtual function table and virtual function table pointer will be automatically generated, and the virtual function table will be replaced with the address of the rewritten virtual function for the rewritten virtual function of the derived class. Thus, when the derived class is assigned to the base class, the base class actually points to the virtual table pointer and virtual function table of the derived class, and calls the virtual function of the derived class.

12. What happens when a virtual function is called in a subclass constructor?

Subclasses will call the parent class construct before calling the subclass construct, so calling a virtual function in a subclass will call the rewritten subclass function if the subclass is rewritten, and will still call the parent class function if it is not rewritten.

  • Note: If you call a dummy function in the parent class construct, the child class will always call the dummy function of the parent class after inheritance, which will result in a linking error if the dummy function is purely dummy.

Also try not to call virtual functions during construction, see Effective C++ clause 9 - Never call virtual functions during construction and destructuring.

13. Introducing move semantics in C++ 11.

The move semantics, i.e., the use of the std::move keyword, in conjunction with the move construct, allows temporary variables to be constructed directly, saving an assignment operation and the destructuring of the temporary variable, which improves efficiency.

14. std::vector What is the difference between push_back and embrace_back for inserting an element?