aikhs
aikhs

Reputation: 979

Does the class object grows in size as its private members grow?

I am writing a class for my SLAM algorithm and this is my first large C++ project! I do remember that std::unique_ptr should be used when I want to keep some object which should have a dynamic memory, one owner and a long lifetime. So when designing a specific class which its object is being created only once and should have a global lifetime (it is the core class object that holds the map). So my idea was to create the std::unique_ptr which will hold that object:

class Backend
{
private:
    std::vector<double> values;
    /// some members
public:
    Backend() : values{0} {}
    ~Backend(){}
    // some functions
};

auto backend_ptr = std::make_unique(Backend());

So my question is: Does the size of backend_ptr will grow if I will increase the size of its private member values overtime? And with your suggestion, do I even need this unique_ptr at all?

Upvotes: 4

Views: 139

Answers (1)

Abyl Ikhsanov
Abyl Ikhsanov

Reputation: 166

The size of the object is determined during the compilation and has nothing to do with the dynamically allocated memory of your std::vector values. This is because during the compilation, your compiler will allocate a fixed memory for the pointers which will point to your variables and objects. So in your case, the pointer to your std::vector.

Regarding whether it is feasible to use std::unique_ptr, it is hard to say without seeing the overall design but my personal choice would be to avoid in your case.

Upvotes: 5

Related Questions