SFML community forums
Help => Graphics => Topic started by: sludge on July 12, 2010, 05:12:26 pm
-
I have a class that returns an sf::String member variable as a reference like so:
sf::String& GetText()
{ return text; }
I call the function in the following way:
mass.GetText().SetText("mass");
The function properly returns the text variable, but I receive a runtime error when SetText() is called during variable assignment.
Unhandled exception at 0x002f4716 in Basebal sim.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Clearly I'm trying to access an memory location that I'm not allowed to touch.
EDIT:: So I realized that the class really should be handling the manipulation of its own variables independently... I figure that I will just create a SetText function inside the class. I am still curious, however, why this routine won't working properly.
EDIT 2: So I still recieve the same error when I try to perform a similar operation within a class member function:
void SetText(std::string word)
{ text.SetText(word); }
The documentation says that the SetText function takes the type (const Unicode::Text &Text). I'm note exactly sure how to replicate this type. I imagine that my problem stems from this specification.
-
Can you give us the declaration of your class please ?
-
of course I can
Here is the .h file
#include "headers.h"
#pragma once
class VariableLabel
{
public:
VariableLabel(void); // Will set the sprite of each label instance
static bool Initialize(const std::string& ImageFile); // The image is loaded only once
sf::Sprite& GetSprite()
{ return sprite; }
sf::String GetText() const
{ return text; }
double GetValue() const
{ return value; }
void SetText(std::string word)
{ text.SetText(word); }
void SetPosition(float x, float y)
{ text.SetPosition(x, y); }
void SetValue(double value)
{ value = value; }
private:
static sf::Image image; // Each sprite object shares this single image
sf::Sprite sprite; // But there is one sprite per variable label
static bool initializationState; // Ensure initialization occurs only once
double value;
sf::String text;
};
And here is the .cpp file
#include "VariableLabel.h"
VariableLabel::VariableLabel(void)
{
//We only want to load the image once
if (!initializationState)
{
Initialize("Resources/VariableLabel.png");
initializationState = true;
}
// Every sprite uses the same image
sprite.SetImage(image);
}
bool VariableLabel::Initialize(const std::string& ImageFile)
{
return image.LoadFromFile(ImageFile);
}
sf::Image VariableLabel::image;
bool VariableLabel::initializationState = false;
-
of course I can
Here is the .h file
sf::String GetText() const
{ return text; }
This method is not returning a reference, plus it is a const method...
Try to modify this.
-
NB : returning references (or pointers) breaks encapsulation.
-
NB : returning references (or pointers) breaks encapsulation.
I wouldn't formulate it that hard. There are many cases in which it is appropriate to return references or pointers.
// Returns the target, or NULL, if nothing targeted
Ship* Ship::GetTarget() const;
// Returns a const-reference to avoid useless copy
const sf::String& Person::GetName() const;
// Returns the reference for the element at index
T& operator[] (size_t index);
But you are right, one shouldn't recklessly return references or pointers in a way that the caller has full access to implementation details.
-
Yes, you're right. But here it's not a good idea to return a ref'.