[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Quick question on explicit typedefs
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Quick question on explicit typedefs

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Terry   USA  (87 posts)  [Biography] 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 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. :)
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Sat 21 Nov 2009 09:30 AM (UTC)
Message
struct S { ... };

and
typedef struct { ... }  S;

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

as Nick pointed out.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Terry   USA  (87 posts)  [Biography] 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 :)
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] 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.


4,876 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]