What are the pros and cons of this class definition style?

Several times I saw the following style class definition. What are the pros and cons of this style?

typedef class _MyClass
{
public :
  _MyClass();
} MyClass;
+3
source share
3 answers

This is quite rare in C ++, but common in C (where it's struct Foonot just an alias Foo.) You can see it in a library that has different classes for different platforms (for example, the Canvas class, which is very implementation specific.) The library will use typedefto allow its users to simplify their code:

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

In this example, you will need to use a type Canvas, not platform-specific types.

+3

++ . C, . .

struct X
{
   int x;
};

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

, struct X a, enum E e; .. C, typedef . .

typedef struct X_ { ... } X;

X a; .

, ++ .

+2

, , , , .

$7.1.3/6- ", , , typedef, . [

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(){}
+2

Source: https://habr.com/ru/post/1774890/


All Articles