dimanche 19 août 2018

Unable to store derived class in a vector of pointer of base classes

I have this collection:

vector<unique_ptr<Light>>* lights;

I have many descendants of the Light class, like DirectionalLight, PointLight and so on.

I wish to store all descendants of Light within that lights vector like so:

template<typename T>
unique_ptr<T> CreateLight()
{
    static_assert(std::is_base_of<Light, T>::value, "Type must of descendant of type Light. ");
    unique_ptr<T> light(new T());
    lights->emplace_back(light);
    return light;
}

The reason for this method is that I store my light in a collection for my renderer, which will do its magic to make the lights affect the shaders.

EDIT

These collections are parts of a class named Scene. I need them all the time and I need to have all Light instances on the heap (together with all the other instances the Scene class has). Every frame the Renderer will go through the collection of lights to affect the scene objects' shaders with them. Accessing this vector any given time is of paramount importance.

I still need a reference to my light in the scene though so I can manipulate its properties.

The error message is this:

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'std::unique_ptr<Light,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': cannot convert argument 1 from 'std::unique_ptr<DirectionalLight,std::default_delete<_Ty>>' to 'std::nullptr_t'

This fails during build, not runtime. I, of course, took a look at answers like this one but to no avail.

I require assistance to get this sorted out.

Aucun commentaire:

Enregistrer un commentaire