Re: why use typedef instead of define?

From: Lisa Pearlson (no_at_spam.plz)
Date: 10/18/04


Date: Mon, 18 Oct 2004 05:24:07 +0200

Cool, never thought about this before.
So typedef is definitely the recommended way.

"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:8sc6n01pqc77mpqjo7mic17rs5a3fbrie0@4ax.com...
> Lisa Pearlson wrote:
>
> >Hi:
> >
> >what's the difference between:
> >
> >typedef sometype mytype;
> >
> >and
> >
> >#define mytpe sometype
>
> Macros use textual substitution, while typedefs do not. Here are a couple
of
> cases where it matters:
>
> #define INTP_MACRO int*
> typedef int* INTP_TYPEDEF;
>
> INTP_MACRO x, y; // x is int*, y is just int
> INTP_TYPEDEF x, y; // Both are int*
>
> const INTP_MACRO p1; // p1 is const int*
> INTP_MACRO const p2; // p2 is int* const
>
> const INTP_TYPEDEF p3; // p3 is int* const
> INTP_TYPEDEF const p4; // p4 is int* const
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++



Relevant Pages

  • Re: Is there a convention mandating macros to be all-uppper-case?
    ... typedef std::wstring tstring; ... to keep namespaces intact. ... Macros are even worse, because they make the substitution completely blindly, without any consideration to their real meaning. ... At least with typedef, you can be sure it's not going to disrupt types in other namespaces, member functions in other classes, etc. ...
    (microsoft.public.vc.language)
  • Re: Why we should (not?) have closures after all
    ... A typedef facility will allow programmers to use ... expressiveness, further decreasing maintenance costs. ... C macros are particularly limited, but a real macro facility is ... how to prevent programmers from doing bad things. ...
    (comp.lang.java.programmer)
  • Re: Use a suffix or a type cast?
    ... I have heard that a suffix is not a good solution and type casts are ... We already had a typedef: ... But much easier than faffing with macros to try and achieve that was: ...
    (comp.lang.c)
  • Re: Operational and functional differences - #define vs typedef ?
    ... Most syntax checking and parsing occurs *after* macros are ... A typedef creates an alias for a type. ... type declarations is such that a macro isn't always going to work. ...
    (comp.lang.c)
  • Alternatives for redefining a typedef?
    ... The PROPERTY macros ... A typedef inside BEGIN_CLASS is almost good, ... BEGIN_CLASS would redefine ThisClass, causing a compile error. ... functions, and be redefined later, and also be used as a template ...
    (comp.lang.cpp)