Derived class User-defined Destructor Error [duplicate]

6 days ago 15
ARTICLE AD BOX

The base class and its derived class have different destructor names

Destructors don't have names. Like constructors, they're referred to via the name of the type being constructed or destroyed, but they are not regular functions and don't have regular function names.

Could you explain what happens when a destructor is declared as a virtual function?

It just tells the compiler that the destructor is virtual. Nothing "happens" at declaration time.

If you mean what happens when an object with a virtual destructor is destroyed, then the answer is virtual dispatch.

Unlike a regular function, the dispatch is to the most derived type's destructor, rather than the most derived method override with the same name, because destructors are not regular functions and don't have regular function names.

answered Mar 1, 2025 at 22:46

Useless's user avatar

2 Comments

However, your answer still did not explain how destructors are overridden when their names are different.

2025-05-01T05:43:15.747Z+00:00

Yes, it did. All methods are identified by a combination of name and parameters except constructors (which can't be virtual anyway) and destructors. Even for regular methods, overriding doesn't happen exclusively by name, you're forgetting about overloads.

2025-05-01T07:46:48.227Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article