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

Author Topic: sf::Drawable and const question  (Read 2490 times)

0 Members and 1 Guest are viewing this topic.

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
sf::Drawable and const question
« on: May 18, 2016, 06:32:38 am »
Hi,

I have a class that is inheriting from sf::Drawable and in the draw() pure virtual function that I am defining for this class I decided that I wanted to loop through a vector of objects and draw some sf::Text output by re-using the same member object and just changing the string, position, and then drawing it.

I soon ran into problems because the draw() function is declared as const.

void gui_table::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        // apply the transform
        states.transform *= getTransform();

        // draw the table background
        target.draw(m_vertices);

        // draw the column names
        std::vector<column>::iterator itr;

        for (itr = m_column_headers.begin(); itr != m_column_headers.end(); ++itr) {
                m_text.setPosition(itr->rect.left + 5, itr->rect.top + 5);
                m_text.setString(itr->value);
                target.draw(m_text);
        }
}
 

Severity   Code   Description   Project   File   Line   Suppression State
Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<gui_table::column>>>' (or there is no acceptable conversion)   Dark Ruins   C:\Users\Erdrick\Documents\Visual Studio 2015\Projects\Dark Ruins\Dark Ruins\gui_table.cpp   64   

This got me to thinking about why draw() is declared as const?  I guess we should not expect to change things here, but I was thinking which of several approaches would be best.

1.) Should I be setting up a vector of sf::Text ahead of time to draw before i get to the draw() function?
2.) Should I make my own draw() function that is not const?
3.) Is there another recommended approach for what I am trying to do?



Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: sf::Drawable and const question
« Reply #1 on: May 18, 2016, 06:41:56 am »
I would like to add I just made these two class members mutable

        mutable std::vector<column> m_column_headers;
        mutable sf::Text m_text;
 

and now the code works, but I'd still like feedback on the approach.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Drawable and const question
« Reply #2 on: May 18, 2016, 07:57:14 am »
Quote
This got me to thinking about why draw() is declared as const?
To enforce a design. Draw should always be const, and decoupled from logic update.

Quote
1.) Should I be setting up a vector of sf::Text ahead of time to draw before i get to the draw() function?
That's a valid option.

Quote
2.) Should I make my own draw() function that is not const?
Could work, but since there are ways to make it work with Drawable::draw... ;)

Quote
3.) Is there another recommended approach for what I am trying to do?
Use a local sf::Text object. There's no reason to keep a persistant instance since you change it completely every time you use it; nothing can be cached.

Quote
I would like to add I just made these two class members mutable
You don't have to declare m_column_headers mutable, because you never modify it in the draw function. Just use a const_iterator.
For m_text, this is a good sign that it shouldn't be a member ;)
Laurent Gomila - SFML developer

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: sf::Drawable and const question
« Reply #3 on: May 18, 2016, 03:55:45 pm »
Thanks a lot Laurent, this helps.  I'll take it from here and make it work!

 

anything