boost::openmethod::weak_virtual_ptr::weak_virtual_ptr

Construct from a shared_virtual_ptr to a derived class

Synopsis

Declared in <boost/openmethod/interop/std_weak_ptr.hpp>

template<class Other>
requires std::is_constructible_v<
            std::weak_ptr<Class>, const std::shared_ptr<Other>&>
weak_virtual_ptr(virtual_ptr<std::shared_ptr<Other>, Registry> const& other);

Description

Copy the v‐table pointer from other. Construct the std::weak_ptr from the std::shared_ptr held by other.

Other is not required to be a polymorphic class: the v‐table pointer is already known.

Example

struct Animal {
    virtual ~Animal() = default;
};
struct Dog : Animal {};
struct Cat : Animal {};

BOOST_OPENMETHOD_CLASSES(Animal, Dog, Cat);

shared_virtual_ptr<Animal> animal = make_shared_virtual<Dog>();
weak_virtual_ptr<Animal> observer = animal;

std::cout << poke(observer.lock()) << "\n"; // bark

animal = nullptr;
std::cout << std::boolalpha << observer.expired() << "\n"; // true

Requirements

  • std::weak_ptr<Class> must be constructible from const std::shared_ptr<Other>&.

Parameters

Name

Description

other

A shared_virtual_ptr to an object of a class derived from Class.

Created with MrDocs