๐ŸŽ‰ Celebrating 25 Years of GameDev.net! ๐ŸŽ‰

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

References can't be used with more than one variable.

Started by
24 comments, last by Josheir 4ย years, 1ย month ago

Well, very interested in the last question still, however, I have found the problem in my code. I was wondering if it was a reference problem and became very confused. ?

Thanks,

Josheir

Advertisement
void Foo(sf::sprite * p_sprite);
Foo(& anotherSprite);

You're becoming confused because C++ reuses the * and the & for different but related things.

Above, the * when used with a type (sf::sprite here) means pointer declaration. So this function takes a pointer argument.

The & when not used with a type (also as above Foo call) means โ€˜address ofโ€™ an object. Since a pointer is just an address, you can use the & to send the address of an object to a function expecting a pointer.

And you've seen that & means Reference declaration when used with a type. 2 different but related uses. Kinda sucks but once you get used to it, it's fine.

To make it even more confusing, you have pointer dereferencing using * ; you can just think of this as โ€˜the value atโ€™ some pointer.

Honestly, this is a lot to understand in such a short space. Find a good pointer/reference tutorial and go into it knowing that * and & mean different things depending on how/where they are used.

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

@fleabay I was just getting back to here to say I am understanding the need for decoupling now. I need all my drawing in one place so I can use clear() and display() adequately as well as making everything pretty. Thanks so much!

@Josheir That's great.

Originally I was thinking you were passing around pointers to raw image data in your game code. I'm not sure how the SFML Sprite class works but I'm sure it's better to pass those object ref/pointers around game code rather than passing around raw data ref/pointers.

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

Why is it better to pass those object ref/pointers around game code?

Thanks,

Josheir

This topic is closed to new replies.

Advertisement