【发布时间】:2014-08-07 00:20:39
【问题描述】:
我刚刚使用#define 来更改我的值,例如:
#include <iostream>
#include <string>
#define Changed_num 100
using namespace std;
int main ()
{
cout<< Changed_num<< endl;
}
但我听说没有#define 有更好的方法(另一种方法是什么?)
所以我让我问一下,那你为什么需要#define?它最常见的用途是什么?
【问题讨论】:
-
它should not be used that way。使用
constexpr int changed_num = 100; -
如果你只是定义一个数字,最好使用常量。 #define 确实很强大,但也有缺陷。所以在使用的时候一定要小心
-
@armanali 不应该反过来:它有缺陷但真的很强大? :)
-
我发现它最常用于使代码更难阅读和调试。 :)
标签: c++