const_cast, how do you use it?

I can't figure this out, and it's really bothering me, this is what I've got using SFML2.

1
2
3
4
5
6
7
8
9
10
11
12
13
void SetChainColor(int PosX, int PosY, std::vector<sf::Sprite*> &BubbleSprites, int BubbleMap[15][15])
{
    //many other lines

    sf::Sprite* Bubble = BubbleSprites[BubbleMap[PosX][PosY] - 1];

    sf::Texture *BubbleTexture;
    const_cast<sf::Texture*> (*BubbleTexture);
    BubbleTexture = Bubble->getTexture();
    sf::Image BubbleImage = BubbleTexture->copyToImage();
    BubbleImage.getPixel(16, 16);

    //many other lines 


X:\Programming\C++\Projects\SFML Bubble Breaker Test\main.cpp|286|error: invalid const_cast from type 'sf::Texture' to type 'sf::Texture*'|

X:\Programming\C++\Projects\SFML Bubble Breaker Test\main.cpp|287|error: invalid conversion from 'const sf::Texture*' to 'sf::Texture*'|

line 286 = line 8
line 287 = line 9
Last edited on
What is the purpose of line 8?

BubbleTexture is already a pointer to a Texture.

My suggestion:

rewrite lines 7 -9 to this:
sf::Texture const * BubbleTexture = Bubble->getTexture();

line 9 error was because you were trying to assign a pointer to a constant Texture (Bubble->getTexture()) to a pointer to a Texture (BubbleTexture). Gotta make sure you have that const in there as indicated by the code I posted.
You might be able to get away without using a const cast if you make copyToImage a const function, as it sounds like a read-only function.

Either way, it looks like what you want is this:

sf::Texture *BubbleTexture = const_cast<sf::Texture*>( Bubble->getTexture() );

The way you have it now is you're casting a sf::Texture (*BubbleTexture) to a sf::Texture* which can't be done. On top of that you're not assigning the cast object to anything.
Last edited on
On line 8 you cast a sf::Texture to a pointer type, which isn't allowed.
On line 9 you don't perform a cast and the const pointer returned from getTexture() cannot be implicitly converted to sf::Texture* without a cast.

If you were to do:

const sf::Texture* BubbleTexture = Bubble->getTexture() ;

I think you'd find you don't need a const_cast at all. Just use the right type to begin with.

You could even do:

sf::Image BubbleImage = Bubble->getTexture()->copyToImage() ;

without the need for the intermediate variable.
Last edited on
thanks to all three of you, I can't change copyToImage without editing SFML's source code and recompiling it, it's a library function, not my own.

I decided to use

sf::Texture *BubbleTexture = const_cast<sf::Texture*>( Bubble->getTexture() )

Even though I could make it const, I wanted to know how to use const_cast.
Makes you wonder... how come they added no less than four keywords for casts, and yet they merely recycled [], for lambda functions, and the && logical AND, for rvalues, and for(), for what should have been called foreach(), since it already has a totally different syntax.
Because there's no chance in hell that existing C code uses identifiers like "const_cast" or "reinterpret_cast", while foreach is much more likely to be used (defining a foreach macro breaks parts of Glib, for example).
Even though I could make it const, I wanted to know how to use const_cast.


The larger part of knowing how to use const_cast is knowing when to use it. This is not a place to use it.
I can't change copyToImage without editing SFML's source code and recompiling it

According to this documentation (SFML 2.0), copyToImage is already a const function.

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php#aefc19bcd95565dd2348fd4cec0facddc
Topic archived. No new replies allowed.