c++ - 从 shared_ptr 获取一个元素

标签 c++ vector sfml

我有以下类文件

#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);
}

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

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

最佳答案

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

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

关于c++ - 从 shared_ptr 获取一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47404244/

相关文章:

c++ - const T& value() const 是什么意思?

r - 如何在 R 中创建具有特定间隔的向量?

c++ - 为什么这个 SFML C++ 代码不起作用?

c++ - "Unexpected precompiled header error"是什么意思?

c++ - typedef 结构中的下划线是什么意思?

c++ - 如何更改可变文件名的文件路径?

r - 按名称添加两个向量

c++ - 使用迭代器在 vector C++ 中循环

c++ - 在 Windows 8 上使用 Code::Blocks 设置 SFML

c# - SFML.net 中的 SFML C++ getSize() 和 sprite.move()