Jump to content

  • Log In with Google      Sign In   
  • Create Account


New struct (to me)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Weston   Members   -  Reputation: 127

Like
Likes
Like

Posted 17 February 2002 - 04:18 PM

I was reading through Tricks of the Windows.... yada yada yada... and in one chapter reguarding bitmaps, there is a new structure setup I''ve never seen. I looked through my C++ books and couldn''t find anything similar to it. I suppose it''s more of a C thing, so here it is: typedef struct tagBITMAP_FILE { BITMAPFILEHEADER bitmapfileheader; BITMAPINFOHEADER bitmapinfoheader; PALETTEENTRY palette[256]; UCHAR *buffer; } BITMAP_FILE, *BITMAP_FILE_PTR; I understand the need for typedef and the tag, but the two variables at the bottom have me questioning. In C++, minus the typedef, this would make BITMAPFILE and BITMAP_FILE_PTR objects. But they don''t work like objects in the previous definition, but rather like a type instead. What I want to know is what is going on here, and is there a more C++ like way to rewrite the structure? I''d like to be able to work with a more familiar syntax.

Sponsor:

#2 davidko   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 February 2002 - 04:28 PM

Just a shorthand way of defining types (note the keyword typedef)

another way is to write:

struct tagBITMAP_FILE
{
BITMAPFILEHEADER bitmapfileheader;
BITMAPINFOHEADER bitmapinfoheader;
PALETTEENTRY palette[256];
UCHAR *buffer;
};

typedef struct tagBITMAP BITMAP_FILE;
typedef struct tagBITMAP *BITMAP_FILE_PTR;



#3 LessBread   Moderators   -  Reputation: 1411

Like
Likes
Like

Posted 17 February 2002 - 04:28 PM

quote:
they don't work like objects in the previous definition, but rather like a type instead

Exactly!

As types there is no longer a need for the "struct" keyword

BITMAP_FILE bmp;
BITMAP_FILE_PTR bmp_ptr = &bmp;


quote:
is there a more C++ like way...


Sure, turn the struct into a class.





Edited by - lessbread on February 17, 2002 11:30:14 PM

#4 Fruny   Moderators   -  Reputation: 1653

Like
Likes
Like

Posted 17 February 2002 - 05:52 PM

Imagine you are declaring a variable ''i'' to be of type int.
int i; // ''i'' is an ''int'' 

If you add the keyword typedef, you suddenly declare ''i'' to be the type int.
typedef int i; // ''i'' means ''int'' 


Now, when declaring a struct, you can immediately declare a variable of the struct type by putting variable names after the struct declarations, just as you do with ''int'' or other types.
struct TAG {...} var1, var2, *var3; 


Furthermore, when you know you won''t need other variables of that type, you can omit the tag and declare an anonymous struct :
struct {...} var1, var2, *var3; 


Remember that, as far as pointer declarations go, the ''*'' is associated with the variable name, not with the type (i.e, ''int *i'' really means ''int (*i)'', not ''int* i''). Thus ''int *i, j'' makes ''i'' a pointer to int and ''j'' an int (instead of two pointers).

So the same is true with your struct : BITMAP_FILE and BITMAP_FILE_PTR would be (without the typedef) respectively a variable of type ''struct tagBITMAP_FILE'' and ''pointer to struct tagBITMAP_FILE''. But with the typedef keyword, you are no longer declaring a variable, but a type, so they become aliases for (respectively) ''struct tagBITMAP_FILE'' and ''pointer to struct tagBITMAP_FILE''.

However, as LessBread pointed out, now C++ compilers will let you use the unqualified tag (i.e. tagBITMAP_FILE) as an alias for the full type name (struct tagBITMAP_FILE).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS