Sign in to follow this  

Allegro: using a BITMAP as a member variable, or something

This topic is 4603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello good people! Instead of having my artwork bitmaps as globals, I'm putting them in a class called Picture. I just want to check I'm doing it right. It seems to work. Here's my class definition (my terminology may be dumb here or there): class Picture { public: void Add(BITMAP* fromBitmap, int x, int y); void Draw(BITMAP* toBitmap, int x, int y); private: BITMAP *m_Image; }; As far as I understand things, the bitmap is technically stored outside of the class (or class instance?), but the pointer is stored inside it, and as you can only get at it using the pointer the bitmap is encapsulated. Am I right? So what would happen if I didn't have the *, in other words, if I tried to have the BITMAP itself inside the class? If it would work, would it be efficient?

Share this post


Link to post
Share on other sites
Quote:
Original post by joebarnslondon
As far as I understand things, the bitmap is technically stored outside of the class (or class instance?), but the pointer is stored inside it, and as you can only get at it using the pointer the bitmap is encapsulated.

Am I right?


Think of a class instance as a block of memory. Everything within that block of memory is part of the class instance. But this is just a lnaguage implementation detail. If you wanted to, you could design a language where members of a class are scattered throughout different areas of memory (though I don't know why you'd want to!). The concept of encapsulation exists at a higher level.

Something is 'encapsulated' when it is a member of a class, and is not directly accessible from outside the class instance (technically, only private members are truly encapsulated, as protected members are still accessible from subclasses, but the term is often applied to them anyway). In otherwords, public members are not encapsulated because they are directly accessible from the outside.

When a class member is a pointer, what it points to is probably not encapsulated. It could be, though. For example, maybe the thing pointed to is a private member of another class. Maybe it's a member of the current class. But this is irrelevant. Usually you don't care where the pointee exists, all you are interested in is the pointer.

In this particular case, you are encapsulating a pointer to a BITMAP (assuming the pointer is private), and not the BITMAP object itself. It doesn't matter where the BITMAP memory is. Your pointer can point to many different BITMAP memory locations throughout the life of the program. The BITMAP object itself is not a member of the class. So the answer to your question is - no, the BITMAP is not encapsulated. Only the pointer is. The fact that you can only access the BITMAP through the pointer is irrelevant. If you wanted to, you could declare a global BITMAP instance on the stack (avoiding Allegro's bitmap creation routine) and have the pointer point to it.

Quote:

So what would happen if I didn't have the *, in other words, if I tried to have the BITMAP itself inside the class? If it would work, would it be efficient?


It would increase the size of the memory block for each class instance. Your pointer, on 32 bit systems, adds 4 bytes to each instance. A BITMAP object would add sizeof(BITMAP) to each instance. This isn't an efficiency problem though (unless you're declaring hundreds of class members - but that's really a design problem [smile]).

It's quite common to declare objects as class members rather than pointers. Point/Vertex/Vector objects in games, for example. Which way you go depends upon how you intend to use the member. It's a design decision. Considering that with Allegro, you will always be dealing with BITMAP pointers rather than BITMAP objects, a member pointer makes sense.

Share this post


Link to post
Share on other sites
There will be only one instance of this class, or if there is another it will store a different bitmap showing a different image.

So I guess I leave it as it is then!

Share this post


Link to post
Share on other sites

This topic is 4603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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