C++里两个变量属于不同的头文件comport属于1hport属于2h在m=1的时候两个变量完全相同m!=1的时候不使用comport
和port,可以这样写:
// 1.h #ifndef 1_H #define 1_H
int comport = 1;
#endif
// 2.h #ifndef 2_H #define 2_H
int port = 2;
#endif
// main.cpp #include "1.h"
int main() { int m = 1; if (m == 1) { // 使用comport std::cout << "comport: " << comport << std::endl; } else { // 不使用comport std::cout << "port: " << port << std::endl; } return 0; }
原文地址: https://www.cveoy.top/t/topic/gADP 著作权归作者所有。请勿转载和采集!