boost::openmethod::virtual_ptr::virtual_ptr

Construct a virtual_ptr from another virtual_ptr

Synopsis

Declared in <boost/openmethod/core.hpp>

template<class Other>
requires std::is_constructible_v<
            Class*, typename virtual_ptr<Other, Registry>::element_type*>
virtual_ptr(virtual_ptr<Other, Registry> const& other);

Description

Copy the object and v‐table pointers from other to `this.

Other is not required to be a pointer to a polymorphic class.

Examples

Constructing from a plain virtual_ptr:

// classes not required to be polymorphic
struct Animal {};
struct Cat : Animal {};
struct Dog : Animal {};

BOOST_OPENMETHOD_CLASSES(Animal, Cat, Dog);

Dog snoopy;
virtual_ptr<Dog> dog = final_virtual_ptr(snoopy);

virtual_ptr<Animal> p = dog;

BOOST_TEST(p.get() == &snoopy);
BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);

Constructing from a smart virtual_ptr:

virtual_ptr<std::shared_ptr<Animal>> snoopy =
    make_shared_virtual<Dog>();
virtual_ptr<Animal> p = snoopy;

BOOST_TEST(p.get() == snoopy.get());
BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);

No construction of a smart virtual_ptr from a plain virtual_ptr:

static_assert(
    std::is_constructible_v<
        shared_virtual_ptr<Animal>, virtual_ptr<Dog>> == false);

Requirements

  • Other's object pointer must be assignable to a Class*.

Parameters

Name

Description

other

A virtual_ptr to a type‐compatible object

Created with MrDocs