I've been doing some reading, and testing around a bit on my own, but wanted some more confirmation on what's going on here. I'm gonna do my best to write this up and explain this.
Let's say I have a Class A, it's contents are irrelevant for this example, so we just have Class A. NOW, we have Class B, which carries 3 Class A objects being allocated on the heap using unique_ptr's.
1 2 3 4 5 6 7 8 9 10 11
|
Class B
{
public:
ClassB();
~ClassB();
private:
std::unique_ptr<ClassA> m_obj1;
std::unique_ptr<ClassA> m_obj2;
std::unique_ptr<ClassA> m_obj3;
}
|
Alright, easy enough. Now I'm going to pretend that ClassB is our "main game" class that holds our game loop functions, our input processings, our window creation, etc, etc, it's our game. So our main.cpp looks like this
1 2 3
|
int main(int argc, char* argv[])
{
std::unique_ptr<ClassB> game = std::make_unique<ClassB>(//Whatever we pass to our constructor);
|
So here is my question. Because we're creating our "main class/game" object on the heap also using a uniquite_ptr, does it really make any sense to create those 3 objects of type ClassA on the heap as well using pointers? Wouldn't those 3 objects still be created on the heap because the object they're a part of <ClassB> is being created on the heap?
In otherwords, let's say our ClassB looked like this instead...
1 2 3 4 5 6 7 8 9 10 11
|
Class B
{
public:
ClassB();
~ClassB();
private:
ClassA m_obj1;
ClassA m_obj2;
ClassA m_obj3;
}
|
Notice the only difference between these two examples of ClassB is the method in which we use to declare our 3 objects. The first one creates them on the heap using smart pointers (unique_ptr), while this second example creates those objects on the stack by just creating them. BUT the class they're being used in is being created on the heap.
So ultimately it makes no difference which method we use in THIS example, right?
Sorry if this was a bit long winded, worded badly, and explained poorly. But thanks for any and all input.