C Virtual based classes inherit more, and the copy constructor problem for grandchildren 20

Updated on technology 2024-04-11
6 answers
  1. Anonymous users2024-02-07

    First of all, I don't think the class system you've constructed should be useful in practice. Second, if both of your father classes have copy constructors, then grandson doesn't need grandpa(gs). Again, the problem is that you inherit from both father classes at the same time, and you can try to inherit alone.

  2. Anonymous users2024-02-06

    It is recommended that you take a look at the member functions declared by C++ Primer Public, and each class must have its own constructor. You can look at the basics of this. A friend is just a relationship that indicates that another function is a friend function.

  3. Anonymous users2024-02-05

    In C++, a derived class inherits all the members of the base class, except for the constructor and destructor.

    The friend function is not a class, it just opens a backdoor to the class.

    Originally, the private members of the class cannot be accessed outside the class, but they can be inherited through the friend function. The child class inherits from the parent class, so by default, it inherits the member functions and member variables of the parent class.

    When a child class is initialized, the constructor of the parent class is automatically called first. Only then do you call the constructor of the subclass when you destruct it in reverse order.

    Construction starts at the very root of the class hierarchy, and in each layer, the constructor of the base class is called first, and then the constructor of the member object. Destruction is performed in the exact opposite order of the construction, which is unique, otherwise the compiler would not be able to automate the process. It cannot be inherited, it is determined by their function.

  4. Anonymous users2024-02-04

    This is probably a clerical error, if the pure virtual function of the base class is not defined in the derived class, then the derived class itself becomes an abstract class and cannot be instantiated. If it is not declared, the inherited function is still a pure virtual function. Because it explicitly tells the compiler that the function has an operation name, but points to an invalid address.

    If the pure virtual function of the base class is redeclared or defined in the derived class, then the operation pointer of the function is pointed to the new address, and if the new operation address is not pointed to 0, it is no longer a pure virtual function, and the derived class is no longer an abstract class.

  5. Anonymous users2024-02-03

    Your problem is a special case of the diamond inheritance problem (shown in the figure below) caused by the multi-inheritance mechanism in C++.

    1.The diamond problem is that a member in R1 will have two copies in C1.

    2.When a1 and b1 use virtual to inherit r1, the diamond problem is solved.

    3.The problem arises again when A1 and B1 are overloaded with the same member function in R1, respectively:

    There are two functions in C1 with the exact same signature, one from A1(void A1::p rint( )b1(void B1::p rint( ) When the C1 object calls print, it calls a1::

    print or b1::p rint?It is impossible to judge.

    4.If the void print( ) function is also implemented in C1, then the C1 object will naturally call C1 when it calls print: :p rint( ) problem is solved.

    That's why "not writing a print in class C will produce the error described above".

  6. Anonymous users2024-02-02

    You'll have dynamic bindings this way, and try to write every print() as a virtual function.

Related questions
6 answers2024-04-11

Many classes are derived a lot and take up a lot of resources. >>>More

6 answers2024-04-11

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 : >>>More

20 answers2024-04-11

First, the mode is different.

Class A** is to directly deduct the subscription fee when subscribing, which is usually a "front-end charge" model. The front-end charging model is the most commonly used charging mode for current investors, and the over-the-counter charging mode that we usually subscribe to is generally the front-end charging model. >>>More

6 answers2024-04-11

I don't know if you know anything about memory allocation. >>>More