Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ Programming
➜ General
➜ Quick question on explicit typedefs
Quick question on explicit typedefs
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Terry
USA (87 posts) Bio
|
Date
| Fri 20 Nov 2009 06:23 PM (UTC) |
Message
| Hrm, I have one more question, and this one is a quicky (or at least I think it is ^_^;).
Looking over the code for SWR, I see a lot of instances of something like
typedef enum
{
LOG_NORMAL, LOG_ALWAYS, LOG_NEVER, LOG_BUILD, LOG_HIGH, LOG_COMM, LOG_ALL
} log_types;
While I know that this is necessary in C so that you can do something like log_types log = LOG_NORMAL; , is there any difference at all in C++ between this and the following?
enum log_types
{
LOG_NORMAL, LOG_ALWAYS, LOG_NEVER, LOG_BUILD, LOG_HIGH, LOG_COMM, LOG_ALL
};
I can't see any, but that doesn't mean there aren't, hence why I'm asking. :D
I have the same question regarding struct s. I know that you can just do Struct myStruct; without a typedef , but are there any other differences of which I'm unaware? Thanks in advance. :) | Top |
|
Posted by
| Nick Gammon
Australia (23,102 posts) Bio
Forum Administrator |
Date
| Reply #1 on Fri 20 Nov 2009 08:40 PM (UTC) |
Message
| Well, a typedef creates a type, whereas a struct creates an instance of a type (which might be an anonymous type).
In particular, if you are going to have more than one of something you probably want a typedef (this isn't the case in your example, I know).
For example:
int main (void)
{
typedef struct
{
int a;
int b;
} t;
t x;
t y;
x = y;
return 0;
}
This compiles OK, including assigning y to x, because they are the same type. However this, which creates two structures, without using a typedef, doesn't:
int main (void)
{
struct
{
int a;
int b;
} x;
struct
{
int a;
int b;
} y;
x = y;
return 0;
}
Error:
test1.cpp: In function ‘int main()’:
test1.cpp:41: error: no match for ‘operator=’ in ‘x = y’
test1.cpp:30: note: candidates are: main()::<anonymous struct>& main()::<anonymous struct>::operator=(const main()::<anonymous struct>&)
Here, although the two structs look the same, they aren't really equivalent.
As for the enum, check out the explanation here:
http://bytes.com/topic/c/answers/741829-difference-between-typedef-enum-enum
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #2 on Sat 21 Nov 2009 09:30 AM (UTC) |
Message
|
and
typedef struct { ... } S;
are basically equivalent in C++, however they are very different from
as Nick pointed out. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Terry
USA (87 posts) Bio
|
Date
| Reply #3 on Mon 23 Nov 2009 12:07 AM (UTC) |
Message
| *nod* That wasn't one of the things I was asking about though. :3 I was just asking about typedef enum { <snip> } foo; , typedef struct MyStruct MY_STRUCT; , etc. But thanks for the help :) | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #4 on Mon 23 Nov 2009 09:26 AM (UTC) |
Message
| It looked to me like it was exactly what you asked about, because you asked about:
typedef enum
{
LOG_NORMAL, LOG_ALWAYS, LOG_NEVER, LOG_BUILD, LOG_HIGH, LOG_COMM, LOG_ALL
} log_types;
vs.
enum log_types
{
LOG_NORMAL, LOG_ALWAYS, LOG_NEVER, LOG_BUILD, LOG_HIGH, LOG_COMM, LOG_ALL
};
and then said: "I have the same question regarding structs"
You say you were just asking about: "typedef enum { <snip> } foo;, typedef struct MyStruct MY_STRUCT;, etc."... I'm not really sure what that "etc." is supposed to be. I have to admit that I find your questions to be somewhat confusing because you tend to use imprecise language, leave out parts of the questions, and assume people just know what you're talking about... if you tried to explain yourself a little better, it would help people give more targeted answers, or, at the least I suppose, answer the questions you think you asked instead of the questions they think you asked. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
17,732 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top