3

I have seen following style class definition several times. What exactly are the pros/cons of this style?

typedef class _MyClass
{
public :
  _MyClass();
} MyClass;
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
user467947
  • 291
  • 3
  • 7
  • 1
    First con: `_MyClass` is a identifier reserved for the implementation (compiler + standard libs). In user code it is incorrect to use it. A related question [here](http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) (the question is tagged C, the first answer is both C and C++, explaining the differences) – David Rodríguez - dribeas Nov 15 '10 at 08:47
  • 1
    This is a leftover from a C developer who had not learned how to use C++ correctly. Ignore it. There are absolutely no benefits from type-defing a class or struct in C++ – Martin York Nov 15 '10 at 08:52
  • Also: dont use a prefix _ unless you really know when it is bad: see http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797 – Martin York Nov 15 '10 at 08:53

3 Answers3

3

It's pretty rare in C++, but common in C (where struct Foo does not automatically alias to just Foo.) You might see it in a library that has different classes for different platforms (e.g. a "Canvas" class, which is very implementation-specific.) The library would use the typedef to let its users simplify their code:

#if WINDOWS
    typedef class WindowsCanvas {
        private: HWND handle;
    } Canvas;
#elif MAC
    typedef class MacCanvas {
        private: CGContextRef context;
    } Canvas;
#endif

In such an example, one would only need to use the Canvas type, never the platform-specific types.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • I think your example doesn't prove anything. One could just NAME the classes with Canvas in both cases – Armen Tsirunyan Nov 15 '10 at 06:51
  • 1
    I know that. :) But some people prefer that style. It *does* make a difference in debugging, where the debugger will typically know the name of a class, but not its `typedef` aliases, making it potentially easier to know which implementation is in use. – Jonathan Grynspan Nov 15 '10 at 06:56
2

In C++ there are no pros. This style came from C where you couldn't just use the struct's name as a type. E.g.

struct X
{
   int x;
};

X a; //compiler error
struct X b; //OK

in order to avoid using the elaborated type specifier, like struct X a, or enum E e; etc. in C it is a common practice to typedef the name. E.G.

typedef struct X_ { ... } X;

Now X a; is OK too.

Naturally in C++ there is no need to do this.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Incorrect. In C, `typedef struct foo foo;` is valid. The `typedef struct _foo foo;` idiom is actually a workaround for C++, since C++ has an implicit `typedef` for every `struct`/`class` declaration, so the `typedef` cannot have the same name as the `struct`. – asveikau Nov 15 '10 at 06:53
  • In C++ there are no pros - some explanation will be helpful. – Chubsdad Nov 15 '10 at 06:55
  • 2
    @asveikau: Huh? typedef struct foo foo; is valid in C++ as well – Armen Tsirunyan Nov 15 '10 at 06:56
  • @Chubsdad: The explanation is the rationale of this style in C and the absence of such need on C++. – Armen Tsirunyan Nov 15 '10 at 06:57
  • @Armen Tsirunyan: oh ok. I am not sure, but seem to find $7.1.3/6 as an advantage – Chubsdad Nov 15 '10 at 07:01
  • 1
    @asveikau: That is a common misconception, the short answer is no, there is no implicit typedef in C++, the difference is that after failing to match an identifier in the non-types name space it will proceed to lookup the identifier in the user defined types name space (name space here does not denote a c++ `namespace` but rather a set of identifiers of a given kind in the compiler). See this [answer](http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) – David Rodríguez - dribeas Nov 15 '10 at 08:55
2

One possible advantage is illustrated by a highly contrived example, but since the Standard speaks of it, it must be having an impliciation for sure.

$7.1.3/6- "Similarly, in a given scope, a class or enumeration shall not be declared with the same name as a typedef-name that is declared in that scope and refers to a type other than the class or enumeration itself. [

typedef struct S{} MYS;

int MYS;                     // Error due to $7.1.3/6

struct A{};

int A;                       // No error, subsequent use required fully elaborated name

int main(){}
Chubsdad
  • 24,777
  • 4
  • 73
  • 129