| |

VerySource

 Forgot password?
 Register
Search
View: 1318|Reply: 11

C ++ object model problem

[Copy link]

2

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Switzerland

Post time: 2020-1-19 17:20:01
| Show all posts |Read mode
Regarding the destruction of class objects, I have the following questions:
class A
{
public:
    virtual void Say () {cout << "A" << endl;}
    ~ A () {};

};
class B: public A
{
public:
virtual void Say () {cout << "B" << endl;}
~ B () {}
};
class C: public B
{
public:
virtual void Say () {cout << "C" << endl;}
~ C () {}
};
int main ()
{
C * p = new C;
p-> Say (); // Call C :: SAY ()
p-> ~ C (); // According to the original book, * p becomes a B object
p-> Say (); // B :: SAY should be called, but A :: SAY is actually called, the following are all A :: SAY
p-> B :: ~ B ();
p-> Say ();
p-> A :: ~ A ();
p-> Say ();

return 0;
}
Is it that ~ B () is called in ~ C ()? And ~ A () is called in ~ B ()?
Instead of calling ~ C () first and then ~ B (), ~ A ()?
Or does the compiler decide how to implement it?
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-26 23:27:01
| Show all posts
The C ++ language standard also describes this problem recursively.
In addition to virtual inheritance, the construction and destructor of each class only need to call the construction and destruction of the direct parent class of the class, respectively.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-27 06:18:01
| Show all posts
The C ++ language standard also describes this problem recursively.
Except for the more complicated case of virtual inheritance, the construction and destructor of each class only needs to call the construction and destructor of its direct base class.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-27 14:00:02
| Show all posts
Because the virtual function call can be found through the virtual table when the program is running, the non-virtual function call is determined at compile time.
Except for the constructor, any function is the same. It's not just a fictional function. Where can the landlord not understand? Ha ha.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-27 16:00:01
| Show all posts
delete has two steps:
(1) Call the destructor;
(2) release memory (that is, call operator delete, note: "operator delete", not the delete itself above)

For the first step, of course, it is also a function call, obeying all the rules of the function call, at least the dynamic binding of virtual functions and the static binding of non-virtual functions.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-28 09:00:02
| Show all posts
Haha, there should be an example of "calling the destructor statically" instead of saying "all calls to the destructor are static."

The same way, ordinary virtual functions can also be called statically, such as:
p-> A :: f ();
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-28 10:27:01
| Show all posts
Don't say the page number later, say the chapter, hoho.
I do n’t have a Chinese version. --b
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-28 10:54:01
| Show all posts
virtual is to be able to find the destructor of a specific derived class through the virtual table of the base class.
Reply

Use magic Report

2

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-2-6 23:45:01
| Show all posts
Then I ask again:

When the destructor of the base class is virtual, the destructor of the derived class should be virtual even if it is not declared as virtual?
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-14 12:15:02
| Show all posts
Ha ha. Yes. This is also the same as the general function.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list