Allegro, how do I pass a BITMAP* by reference?
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!
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
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement