#define file type

Is an

#define LBitmap std::list < CBITMAP *>

good practice?

Edit: Well, what can I do to convince my boss that this is bad practice?

+3
source share
5 answers

No, it is not recommended to use #define in C ++.

It is good to use typedef as it has a well-defined area

typedef is defined by scope, and the compiler interprets its definition every time it occurs, and not in #define. #define is interpreted as compilation time.

Here is the definition of MSDN typedef and #define

A typedef is entered in the typedef declaration, which within its scope becomes a synonym for the type specified by the declaration type declaration part

DEFINE .

#define LBitmap std::list < CBITMAP *>   // BAD

typedef std::list < CBITMAP *> LBitmap  // GOOD

#define CHARPTR char*

CHARPTR a, b;

char* a, b;

a char *, b char

typedef

typedef char* CHARPTR;
CHARPTR a, b;

a b char *

+13

, (#define) ++. typedef:

typedef std::list<CBITMAP *> LBitmap;

: , -, Pardeep, fun , :

#define FooRef Foo &

struct Bar {
    Foo a, b;
};

void func(Bar &bar)
{
    FooRef a = bar.a, b = bar.b;
    a = some_value;
    b = another_value;
}
+9

#define - , , , .
  typedef, ..

Here is an example where #define fails!

#define CHARPTR_DEFINE char*  
typedef char* CHARPTR_TYPEDEF;
CHARPTR_DEFINE ptr1, ptr2;   --> evaluates to char *ptr1, ptr2;
CHARPTR_TYPEDEF ptr3, ptr4;  -->evaluates to char *ptr3,*ptr4;
+3
source

No, there is a much better way to defining type aliases:

typedef std::list< CBITMAP *> LBitmap;

A #defineleads to text replacement, which may lead to unexpected results. typedef, on the other hand, creates the correct alias for another type.

+2
source

No. It is better to use typedefin this case as:

typedef std::list<CBITMAP*> LBitmap;
+1
source

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


All Articles