Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Returning sf:String reference from member function  (Read 3157 times)

0 Members and 1 Guest are viewing this topic.

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
Returning sf:String reference from member function
« on: July 12, 2010, 05:12:26 pm »
I have a class that returns an sf::String member variable as a reference like so:

Code: [Select]

sf::String& GetText()
{ return text; }


I call the function in the following way:

Code: [Select]

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:

Code: [Select]
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.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Returning sf:String reference from member function
« Reply #1 on: July 13, 2010, 07:48:05 am »
Can you give us the declaration of your class please ?
Mindiell
----

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
Returning sf:String reference from member function
« Reply #2 on: July 13, 2010, 04:43:12 pm »
of course I can

Here is the .h file
Code: [Select]


#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
Code: [Select]

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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Returning sf:String reference from member function
« Reply #3 on: July 13, 2010, 11:22:01 pm »
Quote from: "sludge"
of course I can
Here is the .h file
Code: [Select]
sf::String GetText() const
{ return text; }

This method is not returning a reference, plus it is a const method...
Try to modify this.
Mindiell
----

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Returning sf:String reference from member function
« Reply #4 on: July 14, 2010, 09:43:16 am »
NB : returning references (or pointers) breaks encapsulation.
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Returning sf:String reference from member function
« Reply #5 on: July 14, 2010, 09:35:04 pm »
Quote from: "Hiura"
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.
Code: [Select]
// 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Returning sf:String reference from member function
« Reply #6 on: July 14, 2010, 09:39:47 pm »
Yes, you're right. But here it's not a good idea to return a ref'.
SFML / OS X developer