从 shared_ptr 中获取一个元素
Get an element from a shared_ptr
我有以下 class 文件
#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
我尝试在 google 周围搜索,但似乎无法找到任何可以帮助我解决这个问题的东西。我只是没有正确理解 std::shared_ptr
吗?如果我不是那么我将如何使用它。
variables.at()
将 return 类型为 std::shared_ptr<sf::texture>
的对象,它不会隐式转换为 sf::texture&
。您需要使用 operator*
:
取消引用它
sf::Texture& gif::getAt(int index)
{
return *(textures.at(index));
}
我有以下 class 文件
#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
我尝试在 google 周围搜索,但似乎无法找到任何可以帮助我解决这个问题的东西。我只是没有正确理解 std::shared_ptr
吗?如果我不是那么我将如何使用它。
variables.at()
将 return 类型为 std::shared_ptr<sf::texture>
的对象,它不会隐式转换为 sf::texture&
。您需要使用 operator*
:
sf::Texture& gif::getAt(int index)
{
return *(textures.at(index));
}