Virtual base for derivative casting of non-polymorphic type

Base-to-output conversion requires an explicit cast, although either static_castor dynamic_cast. When the database is virtual, only the latter is applied. In addition, it dynamic_castcan only be used for polymorphic types. Together, they seem to suggest that it is practically impossible to convert a virtual base into a derivative, given that the type involved is not polymorphic. It's true?

+4
source share
2 answers

Your interpretation of the standard looks correct.

However, I am ready to say that it does not matter, because your hypothetical virtual database with a non-virtual destructor is a disaster waiting for someone to try to delete it polymorphically and get into undefined behavior, because the destructor is not virtual.

+2
source

Dynamic / static casts apply only to pointers and links.

When the database is virtual, both static and dynamic quotes are used.

class Base {
public:
  virtual ~Base() {};
};

class Derived : public Base {
};

int main(int argc, char **argv)
{
  Base *b = nullptr;
  Derived *d = nullptr;
  d = dynamic_cast<Derived *>(b);
  d = static_cast<Derived *>(b);

  return 0;
}

The second part of the question: if the base is virtual, the derived type is polymorphic. What do you mean exactly?

-1
source

All Articles