【问题标题】:Get an element from a shared_ptr从 shared_ptr 中获取元素
【发布时间】:2017-11-21 02:29:18
【问题描述】:

我有以下类文件

#pragma once
#include <memory>
#include <iostream>
#include <SFML/Graphics.hpp>

class gif
{
    public:
        gif(const std::vector<std::shared_ptr<sf::Texture>>& textures);
        sf::Texture getAt(int);
    private:
        std::vector<std::shared_ptr<sf::Texture>> textures;
};

gif::gif(const std::vector<std::shared_ptr<sf::Texture>>& c)
{
    textures = c;
}

sf::Texture& gif::getAt(int index)
{
    return textures.at(index);
}

可变纹理似乎不像传统矢量那样工作,并且没有at(int) 函数来指向我的矢量中的元素。我如何能够使用integer 指向textures 中的某个sf::Texture

我尝试在谷歌搜索,但似乎无法找到任何可以帮助我的东西。我只是没有正确理解std::shared_ptr吗?如果我不是,那我将如何使用它。

【问题讨论】:

    标签: c++ vector sfml


    【解决方案1】:

    variables.at() 将返回std::shared_ptr&lt;sf::texture&gt; 类型的对象,它不会隐式转换为sf::texture&amp;。您需要使用 operator* 取消引用它:

    sf::Texture& gif::getAt(int index)
    {
        return *(textures.at(index));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2011-11-09
      • 2018-09-02
      • 2010-11-08
      • 2018-08-06
      • 2012-03-30
      相关资源
      最近更新 更多