What is a virtual base class and what does it do

Updated on healthy 2024-02-27
6 answers
  1. Anonymous users2024-02-06

    For the inheritance of the class.

    class derive : virtual public base{};The virtual base class mainly solves the problem that the base class may be inherited multiple times when there is multiple inheritance, and the virtual base class mainly provides a base class to the derived class, such as: class b{}; class d1 :

    public b{};class d2 : public b{};class c: public d1, public d2{};Here c inherits on d1, d2, but there are two base classes, creating confusion.

    Therefore, the virtual base class is used, i.e., class b{}; class d1 : virtual public b{};class d2 :

    virtual public b{};class c: public d1, public d2{};The metaphor in the easy-to-understand MFC is good, at least that's what Apple understands.

  2. Anonymous users2024-02-05

    A virtual base class mainly provides a base class to a derived class.

    The virtual base class is shared, that is, no matter how many times it is inherited in the inheritance system, there will only be one child of the virtual base class in the object memory model (which is completely different from multiple inheritance), so since it is shared, each subclass will not be exclusive, but there must always be a class to complete the initialization process of the base class (because all objects must be initialized, even if it is the default), and the initialization cannot be repeated.

  3. Anonymous users2024-02-04

    There is a common base class on multiple inheritance paths, and at the confluence of several paths, this common base class will produce multiple instances (or multiple copies), if you only want to save one instance of this base class, you can describe this common base class as a virtual base class.

    Virtual inheritance is a technique in object-oriented programming that refers to a specified base class that, in an inheritance architecture, shares instances of its member data with other classes that are also derived directly or indirectly from that base type.

    Virtual inheritance is a concept unique to multiple inheritance. Virtual base classes emerged to solve multiple inheritances.

  4. Anonymous users2024-02-03

    A virtual function is used to modify the parent class function in virtual in polymorphism to ensure that the parent class function runs when the parent class pointer calls the child class object. Hugh. The virtual base class is used in multiple inheritance, if the parent class inherits from the same parent class, only one parent class is instantiated (a bit of a twist, that is, only one grandfather is instantiated.

    The purpose of setting up a virtual base class is to improve the efficiency of the program. When there is a common base class on multiple inheritance paths, at the confluence of several paths, the common base class will produce multiple cover instances, if you only want to save one instance of this base class, you can describe this common base class as a virtual base class.

    Calculate the area. The virtual base class is built in the derived class, and before the derived class itself. When there is a derived class 1, a derived class 2, and a generated derived class 12, the derived class 12 executes the construction of the virtual base class only once.

    There is only one object in the virtual base class. As for what special purpose it has, I didn't expect it.

    That's right. The main thing is to avoid that when the diamond is inherited, there are 2 copies of the grandfather class in the grandchild class. Specifically, look at the encyclopedia "virtual base class".

    By purely virtual functions, it can be indicated that a virtual function only provides an interface that can be overridden by subtypes. A pure virtual function is a virtual function declared in the base class, it can be defined in the base class, and the derived class must define its own implementation.

  5. Anonymous users2024-02-02

    The purpose of setting up a virtual base class is to improve the efficiency of the program.

    When there is a common base class on multiple inheritance paths, at the confluence of several of these paths, the common base class will produce multiple instances, if you only want to save one instance of the base class, you can describe the common base class as a virtual base class.

    The reason for the ambiguity in inheritance may be that the inheritance class inherits the base class multiple times, as shown in the overview diagram, and subclass c ends up accepting the same copy or copies from A and B, respectively, resulting in multiple copies, i.e., multiple copies of the base class members are created in memory by inheriting the class through multiple paths more than once.

    And these are inherited from the parent class by A and B, so should Class C inherit from A or B or accept both? This creates ambiguity, and the basic principle of a virtual base class is that there is only one copy of the base class member in memory. In this way, by declaring the base class inheritance as virtual, only one copy of the base class can be inherited, thus eliminating ambiguity.

    Calls to the constructor of the virtual class are listed in the member initialization list of the constructor in a derived class that is directly or indirectly derived from a virtual base class. However, the constructor of the virtual base class is only called with the constructor of the farthest derived class from which the object was established, and the calls to the constructor of the virtual base class listed in all base classes of that derived class are ignored in execution.

  6. Anonymous users2024-02-01

    c states that normal method overloading means that two or more methods in a class (including hidden, inherited methods) have the same name, and the compiler knows which method should be called in which case as long as the type or number of arguments used is different.

    However, when redefining this virtual function in the derived class, the method name, the return value type, the number of parameters in the parameter table, the type, and the order must be exactly the same as the virtual function in the base class.

    To put it simply, the override method in the subclass can override the virtual method in the accumulation, and when you convert an instance of a subclass to the base class, the method is still called by the override method of the subclass.

    Two functions with the same name in a class are called "overrides", and functions with the same names in the base class and inheritance classes are called "overrides", and virtual functions must be used for rewriting.

    Only the method of the base class can be override by the derived class after adding the keyword virtual, so as to achieve the most important feature of object orientation - polymorphism, that is, the base class can use the method of the derived class.

    父類中方法重載,提供某行為的不同篦現方法(傳入不同的參數);

    The reload of the parent corresponding method in the subclass can be understood in this way (but the actual compilation process is not like this) after the subclass completely copies the parent code, the part of the override code is dug up and supplemented with the new code of the subclass. If the subclass doesn't have a reload (doesn't use the override keyword) and has exactly the same method as the parent, it can be understood that the subclass doesn't dig up the code, it just simply overlays it When you want to use the inherited code, you only need to convert the type to float the overturned code.

Related questions
15 answers2024-02-27

Static --- static.

Here's an example. class t >>>More

7 answers2024-02-27

Mixed rare gem material is known as "mixed gemstone" material, which is composed of a variety of rare complex crystals and minerals. >>>More

6 answers2024-02-27

Upstairs. It's very clear!

I don't need to explain it anymore! >>>More

11 answers2024-02-27

Poria cocos, commonly known as Yun Ling, Pine Cocos, and Poria Cocos, is a fungus plant parasitic on the roots of pine trees, shaped like a sweet potato, with a black-brown skin and a white or pink inside. Poria cocos contains Poria polysaccharides, glucose, proteins, amino acids, organic acids, fats, lecithin, adenine, choline, ergosterol, a variety of enzymes and potassium salts. >>>More

10 answers2024-02-27

Building pay-off is the process of determining its location and its components in the construction process of building engineering. >>>More