0

I'm quite sure I'm not creating a pure 'Object' variable. I am declaring a vector consisting of Objects though, but I believe that should be legal even though 'Object' it is a abstract class/struct?

Header file:

#pragma once

#include <glm/glm.hpp>
#include "Collider.h"

#include "Renderer.h"

struct Object
{
public:
    float mass = 10;
    float Resititution = 0.7;
    glm::vec2 velocity;
    glm::vec2 force = glm::vec2(0, 0);
    bool isDynamic = true;
    bool isKinematic = true;
    
    Collider* collider;
    Transform* transform;

    virtual void submitToRenderer(Renderer* renderer) const = 0;
};

struct SphereObj : Object
{
    void submitToRenderer(Renderer* renderer) const override;
};

struct PlaneObj : Object
{
    void submitToRenderer(Renderer* renderer) const override;
};

cpp file:

#include "Object.h"

void SphereObj::submitToRenderer(Renderer* renderer) const
{
    renderer->submitObject(this);
}


void PlaneObj::submitToRenderer(Renderer* renderer) const
{
    renderer->submitObject(this);
}

in main()

std::vector<Object> objList; 

in another file:

std::vector<Object*> m_objects;
bonkt
  • 15
  • 1
  • 9
  • 1
    _"... I am declaring a vector consisting of Objects...but I believe that should be legal..."_ let's see the code for this. But in general no, the `std::` collections hold objects by value. Please read [ask] with a [mcve]. – Richard Critten Jun 16 '21 at 19:04
  • Please edit your question to show how you are using `Object` and derived types. I suspect you might be hitting [object slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing). – G.M. Jun 16 '21 at 19:04
  • `std::vector objList;` holds just `Object`s, slices any derived classes and needs `Object` to be constructible (which it is not) and copyable. See @G.M. 's comment re slicing. – Richard Critten Jun 16 '21 at 19:11
  • OK, so declaring a container of an abstract class, and populating it with sub-classes only works if it is a pointer to that subclass? How would I go about doing this then? If I want to store all my objects in one container, regardless which of the *subclasses* of the object? – bonkt Jun 16 '21 at 19:14
  • 1
    _"...How would I go about doing this then?..."_ `std::vector> objList;` Assuming `objList` is the owning container. – Richard Critten Jun 16 '21 at 19:18
  • Then "dereference" the smart pointers and apply the data to them, and then push_back() in the vector? huh, I never thought about it this way, I somehow believed I needed a non-pointer place to store them at and then have all other pointers copy from there. – bonkt Jun 16 '21 at 19:25

2 Answers2

0

Looking at your code, I guess that you are working in a 3D engine [Game Engine].

So, you should instantiate your Object like this:

Object* object = Instantiate<SphereObj>(...); 

I have assumed that you have implemented your Insantiate template function.

Ravi Prakash
  • 313
  • 1
  • 8
  • I guess I'm trying to build a game engine, or atleast a game. But I'm far from done, and not even sure why that template function would be needed, Any sources where I can read up on it? – bonkt Jun 16 '21 at 19:26
  • You can check this Youtube playlist: https://www.youtube.com/watch?v=JxIZbV_XjAs&list=PLlrATfBNZ98dC-V-N3m0Go4deliWHPFwT – Ravi Prakash Jun 16 '21 at 19:31
  • The cherno is a great channel for learning Game Engine Developement. By the way, every engine has its own object instantiation methods. For example, you can look at the unity engine API there is an Instantiat method that instantiates any game object or prefab into the currently loaded and active scene. – Ravi Prakash Jun 16 '21 at 19:33
  • This won't solve the `std::vector` problem. –  Jun 16 '21 at 20:03
0

std::vector uses the new operator to allocate it's objects. If you have std::vector<Object> objList, the std::vector is going to allocate objects for it. Object, however, is an abstract class, so when new attempts to allocate and initialize these objects, it won't work.

An Object* is a different, thing, as it is a pointer, not the abstract class itself. This will work, because the pointer is not an abstract class.