How to Add Constructors/Destructors to an Unnamed Class

How to add constructors/destructors to an unnamed class?

You can not declare a constructor or destructor for an unnamed class because the constructor and destructor names need to match the class name. In your example, the unnamed class is local. It has no linkage so neither mangled name is created.

C++ how to add destructor to anonymous class?

This cannot be done in C++. However, the real C++ analogue of anonymous classes is called an anonymous namespace:

namespace {
struct foo {
// ... whatever
~foo();
};
}

// ... later in the same C++ source.

foo bar;

Now you can use and reference foos everywhere in this specific C++ source file. Other C++ source files may have their own anonymous namespace, with their own foos, without creating a conflict. The end result is pretty much the same thing as C-style anonymous structs (a.k.a. classes), except that they're not really anonymous, only their namespace is.

Class with no name, constructor, destructor

If the class is unnamed then it can't have a custom constructor/destructor (although the compiler will generate the usual default ones for you). That means you can't pass an argument to it at construction. However, there's probably nothing stopping you from either (1) adding one or methods to the class to pass data to it after construction, or (2) giving the class a name in the first place.

Anonymous constructor and destructor call in vector

std::vector is a dynamic array. When you add an element and the capacity of its internal buffer is full, vector needs to:

  1. allocate new memory buffer,
  2. copy/move elements from old buffer to new buffer,
  3. destroy elements in original buffer,
  4. deallocate original buffer,
  5. copy/move added element at the end of new buffer.

(Not necessarily in this order.)

Step 2. involves copy constructor you do not understand. Step 3 involves destructor you do not understand.


To prevent this (inefficient) behavior, use std::vector::reserve before inserting elements (if you can). Then no reallocation and no hidden copy/move of elements will take place.

How do I ensure the destruction of a parent instance member?

You need a virtual destructor in a delete expression's statically known class (in your case B) if that class is different from the most derived class of the object (in your case also B). In your case those classes are the same, so you don't need a virtual destructor: everything's destroyed properly. But if you had made the pointer type A*, then you'd need a virtual destructor in A to avoid Undefined Behavior (nasal daemons and such).

Why destructor is not getting called for anonymous objects?

A a = A(1); is equivalent to A a = 1;. However, in both cases, copy elision may occur: The A(1) is actually constructed directly into a, instead of being constructed separately and then copied or moved.

It's up to the compiler to decide whether or not to perform copy elision in any of its permitted scenarios (as described in the above link).

Why can't anonymous unions contain members with non-trivial constructors/destructors?

A bigger reason would be: how would the union know which destructor to call. The language itself doesn't track which member is active in a union.

It seems that C++0x will allow non-trivial types in unions, in which case you'll be forced to implement your own constructor(s) and destructor. (The latter is a little unclear from the proposal, it seems that the union destructor will not call any member destructors and the destructor for the right one would have to be invoked manually.)



Related Topics



Leave a reply



Submit