typedef struct question
What exactly is a struct and what does typedef do? I've seen it several times but I don't really know what exactly it does. Is it similar to classes?
In general, typdefing a struct is legacy C style code to avoid having to say
You have to explicitly tell a c compiler you are using structs. In c++ you don't.
so they typedef it to something like this:
then they stick it in one line because "why make a program readable?".( yeah, I don't know why they do it )
And the above probaly isn't right, I never use this syntax.
struct SomeStruct{/* ... */};struct SomeStruct variable;
You have to explicitly tell a c compiler you are using structs. In c++ you don't.
so they typedef it to something like this:
struct SomeStruct_TAG{/* ... */};typedef SomeStruct_TAG SomeStruct;SomeStruct variable;
then they stick it in one line because "why make a program readable?".( yeah, I don't know why they do it )
typedef struct SomeStruct_TAG{/* ... */}SomeStruct;SomeStruct variable;
And the above probaly isn't right, I never use this syntax.
I assume C++?
A struct is just a class, except the default access level is public.
A typedef is a way to rename a type name. For instance:
typedef std::basic_string< TCHAR > tstring;
jfl.
A struct is just a class, except the default access level is public.
A typedef is a way to rename a type name. For instance:
typedef std::basic_string< TCHAR > tstring;
jfl.
Quote:Original post by LessBread
A struct groups different variables together. A typedef defines a data type.
Unless he's talking about C++, then structs are just classes with public access as the default, instead of private.
Quote:Original post by RoboguyQuote:Original post by LessBread
A struct groups different variables together. A typedef defines a data type.
Unless he's talking about C++, then structs are just classes with public access as the default, instead of private.
I find 'group of variables' to be more explaining than 'classes with public access', from a noob point of view.
Well. *shrugs*
Quote:Original post by RoboguyQuote:Original post by LessBread
A struct groups different variables together. A typedef defines a data type.
Unless he's talking about C++, then structs are just classes with public access as the default, instead of private.
Since he asked about structs and typedefs, it seems more likely that he's asking about straight C - even if he asked if they were similar to classes. Tell me though, does a struct group different variables together under C++ or does it do something different? To be more straight forward, my answer may have been simplistic, but it wasn't wrong. A struct groups different variables together in C and in C++.
Quote:Original post by LessBread
A typedef defines a data type.
I wish it did. But unfortunately a typedef only defines a new name for an existing data type. For example:
/* in my headers */#define SOME_FLAG_SETTING 0x100typedef unsigned int flag_t;/* somewhere in my code */unsigned int flag = SOME_FLAG_SETTING;do_something_with_flags(flag, 200);/* somewhere else */void do_something_with_flags(unsigned int count, flag_t flag){ /* ... */}
In this example, the compiler will let me pass an unsigned int to do_something_with_flags, despite the fact that I've declared do_something_with_flags to accept an argument of type flag_t. This behaviour is, unfortunately, perfectly valid, because flag_t is really only a new name for an unsigned int.
If you do want typesafety in this sort of case, you can have it, but you need to do a bit more work:
/* in my headers */#define SOME_FLAG_SETTING {0x100}typedef struct { unsigned int flag; } flag_t;/* somewhere in my code */unsigned int flag = SOME_FLAG_SETTING;do_something_with_flags(flag, 200);/* somewhere else */void do_something_with_flags(unsigned int count, flag_t flag){ /* ... */}
In the above code, the compiler will raise an error indicating that we're attempting to pass an incompatible type as the second argument of do_something_with_flags.
You can also achieve something similar using enums in C, and C++ presents many more opportunities for writing typesafe code.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement