Quick question on explicit typedefs

Posted by Terry on Fri 20 Nov 2009 06:23 PM — 5 posts, 23,874 views.

USA #0
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 structs. 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. :)
Australia Forum Administrator #1
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
USA #2
struct S { ... };

and
typedef struct { ... }  S;

are basically equivalent in C++, however they are very different from
struct { ... } S;

as Nick pointed out.
USA #3
*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 :)
USA #4
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.