Sign in to follow this  
darenking

Allegro, how do I pass a BITMAP* by reference?

Recommended Posts

Hi, Just a small thing. I'm using Allegro C++. I have a vector of bitmap pointers: vector<BITMAP*> m_Sprites; I just want to be able to change one of these sprites, let's say the third sprite, in a method. So I need to pass it, not by copy of course but by reference or whatever. I know I will be using something like this: m_Sprites[2]. But I don't know about the pointers and/or references. I sorta get the idea of them but can never do the syntax!

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
A reference to a BITMAP * would be a BITMAP * &


Is that legal C++?

Passing it by value here would be the same anyway since it is a pointer.

ace

Share this post


Link to post
Share on other sites
Yes, it's legal C++. And passing it by value is not the same since if you pass it by value you can't alter the pointer value, just the thing that is pointed to.

Share this post


Link to post
Share on other sites
If you merely use m_Sprites[2], you will be passing the pointer to the BITMAP to whatever function you're working with. Your function signature would include a parameter of type BITMAP * (bool DoSomething(BITMAP *sprite_bmp);). The chances are that this would be acceptable, as long as all you need to do is work with the contents of the bitmap.

However, if you need to destroy the bitmap and/or create a new one, or change it to point at a different bitmap, then you would need to either pass by reference (bool DoSomething(BITMAP*& sprite_bmp), my recommendation because your syntax within the function is much simpler, and you can still pass m_Sprites[2]) or add another layer of pointer indirection (bool DoSomething(BITMAP **sprite_bmp), where you need to use *sprite_bmp within the function in most cases, and you must pass &(m_Sprites[2])).

Hope that helps,
Twilight Dragon

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this