为什么是#define坏,什么是正确的替代品? [英] Why is #define bad and what is the proper substitute?

查看:168
本文介绍了为什么是#define坏,什么是正确的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define dItemName        L"CellPhone"


推荐答案

将其定义为常量变量。这是一个很好的编程习惯。

Define it as a constant variable. It is a good programming practice.

const wchar_t *dItemName = L"CellPhone";

如果你需要知道字符串的长度在某个地方,

In case you need to know the lenght of your string somewhere later, then define it as an array:

const wchar_t dItemName[] = L"CellPhone";

此外,为什么#define是坏的:它将所有使用单词dItemName的地方转换为LCellPhone 。示例:

Also, why #define is bad: It transforms all places where you use word dItemName to L"CellPhone". Example:

struct {
  int dItemName;
} SomeStruct;

将无效:

struct {
  int L"CellPhone";
} SomeStruct;

这篇关于为什么是#define坏,什么是正确的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆