如何正确使用函数指针并返回?
How to properly use pointers with functions and returning?
我想在 class 中有一个 sf::Texture 的 (sfml) 数组,然后想要一个 returns 指向该特定项目的指针的函数数组(向量)。
然后我想将其传递给需要 sf::texture 的 sprite.SetTexture(也是 SFML)。
但是我无法让它工作,这就是我目前拥有的,我遗漏了不重要的东西:
class SpriteTextures
{
public:
std::vector<sf::Texture> Textures;
//code to fill the vector here
sf::Texture *GetSecondTexture()
{
return &Textures[1];
}
};
class DrawSprite
{
sf::Texture *texture;
sf::Sprite sprite;
public:
void SetSpriteTexture()
{
SpriteTextures SprTexs;
texture = SprTexs.GetSecondTexture();
sprite.setTexture(texture, true); // Gives error about no suitable constructor exists to convert from sf::Texture * to sf::Texture
// do some other stuff with the texture here.
}
};
这是Sprite::setTexture的函数定义:
void setTexture(const Texture& texture, bool resetRect = false);
它给出的错误我已作为注释放在代码中(在 setTexture 函数旁边)
所以我想要的是,不是将向量项目复制到 DrawSprite class 中的纹理变量,而是希望 DrawSprite 中的 *texture 指向向量中的项目。
但我确实想先在 DrawSprite 中设置 *texture,因为我也想将它用于其他用途。
所以我希望能够为我的 * 纹理提供 setTexture 函数,并让它读取 SpriteTextures class 中向量中的 sf::Texture。(而不是在纹理变量中读取它的副本DrawSprite class 本身。
所以如果有人理解我刚才说的话,有人可以帮助我让它正常工作吗?
谢谢!
基本上,在这个函数中:
void SetSpriteTexture()
{
SpriteTextures SprTexs;
texture = SprTexs.GetSecondTexture();
sprite.setTexture(texture, true); // Gives error about no suitable constructor exists to convert from sf::Texture * to sf::Texture
// do some other stuff with the texture here.
}
您将 sf::Texture*
传递给 sprite.setTexture(..)
,而实际上,它需要一个 sf::Texture&
。只需将调用更改为:
sprite.setTexture(*texture, true);
或者,让您的其他函数简单地 return 成为参考:
sf::Texture& GetSecondTexture()
{
return Textures[1];
}
这意味着您现在 return默认设置 sprite 所需的内容。当然,您必须确保您的纹理容器至少与您的精灵一样长,否则纹理引用将不再有效。
编辑
此外,查看评论,您似乎对 *
运算符和 &
运算符有点困惑。以下是解释这些运算符的各种用例的快速片段:
int a = 42;
int* b = &a; // * Declares pointer. & Takes address of a
int& c = a; // & Declares reference.
*b = 32; // * Dereferences the pointer, meaning you get the value it points to.
// a, and c are now 32, and b points to a, which holds the value 32.
我想在 class 中有一个 sf::Texture 的 (sfml) 数组,然后想要一个 returns 指向该特定项目的指针的函数数组(向量)。 然后我想将其传递给需要 sf::texture 的 sprite.SetTexture(也是 SFML)。 但是我无法让它工作,这就是我目前拥有的,我遗漏了不重要的东西:
class SpriteTextures
{
public:
std::vector<sf::Texture> Textures;
//code to fill the vector here
sf::Texture *GetSecondTexture()
{
return &Textures[1];
}
};
class DrawSprite
{
sf::Texture *texture;
sf::Sprite sprite;
public:
void SetSpriteTexture()
{
SpriteTextures SprTexs;
texture = SprTexs.GetSecondTexture();
sprite.setTexture(texture, true); // Gives error about no suitable constructor exists to convert from sf::Texture * to sf::Texture
// do some other stuff with the texture here.
}
};
这是Sprite::setTexture的函数定义:
void setTexture(const Texture& texture, bool resetRect = false);
它给出的错误我已作为注释放在代码中(在 setTexture 函数旁边) 所以我想要的是,不是将向量项目复制到 DrawSprite class 中的纹理变量,而是希望 DrawSprite 中的 *texture 指向向量中的项目。 但我确实想先在 DrawSprite 中设置 *texture,因为我也想将它用于其他用途。 所以我希望能够为我的 * 纹理提供 setTexture 函数,并让它读取 SpriteTextures class 中向量中的 sf::Texture。(而不是在纹理变量中读取它的副本DrawSprite class 本身。
所以如果有人理解我刚才说的话,有人可以帮助我让它正常工作吗?
谢谢!
基本上,在这个函数中:
void SetSpriteTexture()
{
SpriteTextures SprTexs;
texture = SprTexs.GetSecondTexture();
sprite.setTexture(texture, true); // Gives error about no suitable constructor exists to convert from sf::Texture * to sf::Texture
// do some other stuff with the texture here.
}
您将 sf::Texture*
传递给 sprite.setTexture(..)
,而实际上,它需要一个 sf::Texture&
。只需将调用更改为:
sprite.setTexture(*texture, true);
或者,让您的其他函数简单地 return 成为参考:
sf::Texture& GetSecondTexture()
{
return Textures[1];
}
这意味着您现在 return默认设置 sprite 所需的内容。当然,您必须确保您的纹理容器至少与您的精灵一样长,否则纹理引用将不再有效。
编辑
此外,查看评论,您似乎对 *
运算符和 &
运算符有点困惑。以下是解释这些运算符的各种用例的快速片段:
int a = 42;
int* b = &a; // * Declares pointer. & Takes address of a
int& c = a; // & Declares reference.
*b = 32; // * Dereferences the pointer, meaning you get the value it points to.
// a, and c are now 32, and b points to a, which holds the value 32.