0

I have a project for uni and I must create 3 objects(unique pointers) using a vector. What do I do wrong with this code? Would be really happy if you could help me out with this.

void vAufgabe_1a() {
    const int num_obj = 3;
    
        for (int i = 0; i != num_obj; i++) {
        auto FZ[i] = make_unique<Fahrzeug>(); // FZ[i] doesn't work here. 
        FZ[i].push_back(move(make_unique <Fahrzeug>));
        cout << "Der Name des Fahrzeugs: " << endl;
        cin << FZ[i]->p_sName;

    }
}
3
  • std::vector<std::unique_ptr<Fahrzeug>> FZ; should be ouside the loop. Commented Dec 1, 2021 at 13:11
  • std::move is useless in std::move(std::make_unique<Fahrzeug>()). (it might be needed for std::move(FZ[i]) though). Commented Dec 1, 2021 at 13:13
  • Please show a minimal reproducible example Commented Dec 1, 2021 at 13:33

1 Answer 1

1

I wouldn't make a vector of unique pointers to objects at all (it will A: be harder, B: a double indirection). A vector will already do all the memory managment for you. Live demo here : https://onlinegdb.com/SeLMTl2Zz

#include <iostream>
#include <memory>
#include <vector>

//-----------------------------------------------------------------------------
// (Fahrzeug, but then in English)

class Vehicle
{
public:
    // give each vehicle a unique instance number
    Vehicle() :
        m_id{ ++s_id }
    {
        std::cout << "Constructed vehicle : " << m_id << "\n";
    }

    // show when object is destructed (and why unique_ptr is not needed)
    ~Vehicle()
    {
        std::cout << "Destructed vehicle : " << m_id << "\n";
    }

    std::size_t get_id() const
    {
        return m_id;
    }

private:
    static std::size_t s_id;
    std::size_t m_id;
};

//-----------------------------------------------------------------------------
// initialize id generating number
std::size_t Vehicle::s_id{ 0ul };

// don't use magic numbers in code, give your constants names
const std::size_t number_of_vehicles = 3ul;

//-----------------------------------------------------------------------------

int main()
{
    // create a scope to manage lifecycle of the vector of vehicles
    // so I can show the destruction phase more clearly
    {
        // you can initialize vectors from the constructor
        std::cout << "Creating vector with vehicles\n";
        std::vector<Vehicle> vehicles(number_of_vehicles);

        for (const auto& vehicle : vehicles)
        {
            std::cout << vehicle.get_id() << "\n";
        }

        std::cout << "Destructing vector with vehicles\n";
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

A vector<object> and a vector<unique_ptr<object>> are two very different things. Which one makes more sense depends entirely on the use case and how your object looks like. You shouldn't make absolute statements like "never make a vector of unique pointers to objects at all". There can be reasons to do this.
What I meant is there should be a good reason to add the extra level of indirection. It must be worth the extra effort and complexity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.