I have a vector that holds Objects that are derived classes from a Base class "SceneObject". For this task, I found that I had to use unique pointers, as seen here:
vector<std::unique_ptr<SceneObject>> objects;
To put my objects inside this vector, I allocate memory on the heap with the new keyword:
objects.emplace_back(new Sphere(glm::vec3(0.9, -1.925, -6.69), 0.825, sphereMaterial));
I access functions from these objects in a loop like:
objects[k]->intersect(...);
But my problem is that I want to initialize a variable "SceneObject" that will contain one of the objects stored in this vector to perform some checks later... I'm trying to store the pointer instead, as I think I should be doing, but I'm getting a compiler error when doing this:
SceneObject* object = NULL;
for(...)
object = &objects[k];
The error:
boilerplate.cpp: In function 'int main(int, char**)':
boilerplate.cpp:606:15: error: cannot convert '__gnu_cxx::__alloc_traits<std::allocator<std::unique_ptr<SceneObject> > >::value_type* {aka std::unique_ptr<SceneObject>*}' to 'SceneObject*' in assignment
object = &objects[k];
^
I used this method as seen in this example code for creating my base/derived classes.
Any suggestion as to how I should initialize this variable with my object?