解决的问题
先看一个历程,猜一猜输出是什么?
#include <iostream>
#define LOG(x) std::cout<<x<<std::endl;
void Increment(int value)
{
value++;
}
int main()
{
int a = 5;
Increment(a);
LOG(a);
std::cin.get();
}
大家可能都以为结果输出应该是6,但是不是!!!结果仍然是5,为什么呢?
#include <iostream>
#define LOG(x) std::cout<<x<<std::endl;
void Increment(int *value)
{
(*value)++;
}
int main()
{
int a = 5;
Increment(&a);
LOG(a);
std::cin.get();
}
等价于
#include <iostream>
#define LOG(x) std::cout<<x<<std::endl;
void Increment(int &value)
{
value++;
}
int main()
{
int a = 5;
Increment(a);
LOG(a);
std::cin.get();
}
后两次的结果都对啦!!