0

I have different objects such as Triangle, Plane, Sphere etc. that all inherit from the OBJECTS class. I want to create a vector of these different objects then loop through each of the elements and perform operations on them. I'm trying to make a raytracer and I want to create put a lot of different objects onto the scene but I have no idea how. OBJECTS is an abstract class so I can't make a vector because I can create instances of it. Should I create a completely new class and have all the shape classes inherit from it? Or should I remake the objects class so that it's not abstract anymore? Help appreciated. My next best guess is object pointers.

JerSci
  • 167
  • 1
  • 9

1 Answers1

1

as the comment suggested you could do something like this.

class Object{
public:
    virtual ~Object()= default;
    virtual void foo() = 0;
};

class Pane: public Object{
public:
    ~Pane() = default;
    void foo() override{
        std::cout << "Hello Pane" << std::endl;
    }
};

class Triangle: public Object{
public:
    ~Triangle() = default;
    void foo() override{
        std::cout << "Hello Triangle" << std::endl;
    }
};

int main(){

    std::vector<std::unique_ptr<Object>> objects;
    objects.emplace_back(std::make_unique<Triangle>());
    objects.emplace_back(std::make_unique<Pane>());

    for(auto &object: objects){
        object->foo();
    }

    return 0;
}

Just create a vector of Objects-Pointers. Note that you dont have to use unique_pointers but I would highly suggest it, because you dont need to delete the objects at the end.

The output of this simple program is:

Hello Triangle
Hello Pane
Jarod42
  • 203,559
  • 14
  • 181
  • 302
HWilmer
  • 458
  • 5
  • 14
  • Stupid question but doesnt the vector automatically deallocate memory or does that not work for pointers and results in dangling pointers since pointers point to a memory location? – JerSci Feb 21 '20 at 17:47
  • If you are using raw pointers the you have to call delete on all objects inside the vector that end. So no the memory wont get freed automatically. – HWilmer Feb 21 '20 at 17:49
  • Another stupid question, isn't the destructor automatically called? – JerSci Feb 21 '20 at 17:50
  • Yes it is for objectes on the Stack, but objects created with new are not on the Stack but on the heap so the destructer isnt called if the scope ends – HWilmer Feb 21 '20 at 17:54
  • 1
    Oh okay that makes a lot of sense, thank you. And a clarification, so the smart pointer deletes the object and itself when it goes out of scope, right? Thanks btw. – JerSci Feb 21 '20 at 17:57
  • 3
    `std::move(std::unique_ptr(new Triangle))`: useless `std::move`. Also Prefer `std::make_unique()` over `new`. – Jarod42 Feb 21 '20 at 17:57
  • Yes you are right. Before I moved the Object into the vector but thought I should just create it inline. Good point. – HWilmer Feb 21 '20 at 18:00
  • @JerSci You may find [What is object slicing?](https://stackoverflow.com/questions/274626/what-is-object-slicing) enlightens you to part of the problem. – user4581301 Feb 21 '20 at 18:00
  • Side note: Not all Automatically allocated variables are on the stack. There might not even be a stack. More reading on that: [Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management?](https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and) – user4581301 Feb 21 '20 at 18:02