简体   繁体   English

C++ 向量的唯一指针实践

[英]C++ Vector of unique pointers practice

First of all, here is my code:首先,这是我的代码:

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

class Animal 
{

public:

    virtual void display() =  0;

};

class Dog : public Animal 
{
    std::string name;

public:

    Dog(std::string n) : name(n) {}

    void display() override
    {
        std::cout << "I'm a dog and my name is: " << name << std::endl;
    }

};


class Cat : public Animal 
{
    std::string name;

public:

    Cat() {}
    Cat(std::string n) : name(n) {}

    void display() override
    {
        std::cout << "I'm a cat and my name is: " << name << std::endl;
    }

};


int main()
{
    Dog D1("Spike");
    Dog D2("Razor");
    Cat C1("Cat");

    std::vector<std::unique_ptr<Animal>> vectorOfAnimals;

    std::unique_ptr<Animal> pointer1 = std::make_unique<Dog>(D1);
    std::unique_ptr<Animal> pointer2 = std::make_unique<Dog>(D2);
    std::unique_ptr<Animal> pointer3 = std::make_unique<Cat>(C1);
    std::unique_ptr<Animal> pointer4 (nullptr);

    vectorOfAnimals.push_back(std::move(pointer1));
    vectorOfAnimals.push_back(std::move(pointer2));
    vectorOfAnimals.push_back(std::move(pointer3));
    vectorOfAnimals.push_back(std::move(pointer4));

    for(auto& animals : vectorOfAnimals)
    {
        animals = nullptr;
    }

    if(!vectorOfAnimals[0])
    {
        std::cout << "First element is nullptr!" << std::endl;
        vectorOfAnimals[0] = std::move(pointer1);
    }

    for(auto& animals : vectorOfAnimals)
    {
        if(!animals)
        {
            std::cout << "This is a nullptr!" << std::endl;
        }
        else
        {
            animals->display();
        }
    }

    return 0;
}

I created an abstract class with 2 derived classes.我创建了一个带有 2 个派生类的抽象类。 Then, pushed backed some unique pointers in a vector of unique pointers to base class.然后,在指向基类的唯一指针向量中推回一些唯一指针。 For learning purposes, then, I assigned to all elements of vector nullptr and tried to transfer ownership of first pointer created called "pointer1" to first element of vector, but it doesn't work, first element remains nullptr.然后,出于学习目的,我分配给了向量 nullptr 的所有元素,并尝试将创建的第一个名为“pointer1”的指针的所有权转移到向量的第一个元素,但它不起作用,第一个元素仍然是 nullptr。 Where I'm wrong ?我错在哪里?

std::move is not something you do just to get your code to compile. std::move不是为了让代码编译而做的事情。 It actually has a function, a purpose, a result.*它实际上具有功能、目的和结果。*

The result is that your data has been moved away.结果是您的数据已被移走。 It's gone.它消失了。 It's in the container now;它现在在容器中; pointer1 etc remain unconnected to the pointers in the vector, and (more importantly) no longer point to anything. pointer1等与向量中的指针保持未连接状态,并且(更重要的是)不再指向任何内容。

As such, this makes no sense:因此,这是没有意义的:

vectorOfAnimals[0] = std::move(pointer1);

Think about the word "unique" in the name unique_ptr .想想unique_ptr名称中的“唯一”一词。


* std::move itself doesn't actually move anything. * std::move本身实际上并没有移动任何东西。 But, for our purposes today, close enough.但是,就我们今天的目的而言,已经足够接近了。

Pointer1 ownership is already transferred to vector[0].指针 1 的所有权已转移到向量 [0]。 so resources pointed to by pointer1 is no longer valid and then again set with the same inside if loop.因此,pointer1 指向的资源不再有效,然后在 if 循环内部再次设置相同的资源。 So that will be a null only i guess.所以只有我猜这将是一个空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM