boost::openmethod::virtual_ptr::virtual_ptr

Construct a virtual_ptr from another virtual_ptr

Synopsis

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

Assigning from a plain virtual_ptr:

struct Animal {}; // polymorphism not required
struct Dog : Animal {}; // polymorphism not required
BOOST_OPENMETHOD_CLASSES(Animal, Dog);
initialize();

Dog snoopy;
virtual_ptr<Dog> dog = final_virtual_ptr(snoopy);
virtual_ptr<Animal> p{nullptr};

p = dog;

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

Assigning from a smart virtual_ptr:

struct Animal {}; // polymorphism not required
struct Dog : Animal {}; // polymorphism not required
BOOST_OPENMETHOD_CLASSES(Animal, Dog);
initialize();

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