Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a non-trivial destructor in C++?

Tags:

c++

destructor

I was reading this which mentions destructors being trivial and non-trivial.

A class has a non-trivial destructor if it either has an explicitly defined destructor, or if it has a member object or a base class that has a non-trivial destructor.

In example, I have a class,

class C {
    public:
     ~C(); // not explicitly declared.
};

If C::~C() is implicitly defined does it make a trival dtor?

like image 638
cpx Avatar asked Nov 19 '11 00:11

cpx


People also ask

What is a non-trivial constructor?

If you define a constructor yourself, it is considered non-trivial, even if it doesn't do anything, so a trivial constructor must be implicitly defined by the compiler.

What is a trivial destructor C++?

A trivial destructor is a destructor that performs no action. Objects with trivial destructors don't require a delete-expression and may be disposed of by simply deallocating their storage. All data types compatible with the C language (POD types) are trivially destructible.

What is default destructor C?

The default destructor calls the destructors of the base class and members of the derived class. The destructors of base classes and members are called in the reverse order of the completion of their constructor: The destructor for a class object is called before destructors for members and bases are called.

What is explicit destructor?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .


1 Answers

You are getting your words mixed up. Your example does indeed declare an explicit destructor. You just forget to define it, too, so you'll get a linker error.

The rule is very straight-forward: Does your class have an explicit destructor? If yes, you're non-trivial. If no, check each non-static member object; if any of them are non-trivial, then you're non-trivial.

like image 178
Kerrek SB Avatar answered Nov 15 '22 14:11

Kerrek SB