using声明与using编译指令code
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
namespace king
{
int sunId = 1;
}
namespace xyj
{
int sunId = 3;
}
void testCout01()
{
int sunId = 2;
cout << sunId <<endl;
}
void testCout02()
{
using namespace king;
using namespace xyj;
cout << king::sunId << endl;
cout << xyj::sunId << endl;
}
int main()
{
testCout01();
testCout02();
system("pause");
return EXIT_SUCCESS;
}
双冒号作用域运算符
#define _CRT_SECURE_NO_WARNINGS
解决C4996错误的方法之一
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <stdio.h>
using namespace std;
int num = 1005;
void numCout()
{
int num = 6;
cout <<"num为多少:" << num << endl;
cout << "全局的num为多少:" << ::num << endl;
}
int main()
{
numCout();
system("pause");
return EXIT_SUCCESS;
}
C语言与C++ const修饰全局变量的区别
一、C语言下const修饰全局变量默认是外部链接属性
在C语言下 const 默认是外部链接属性,
所以可以在其他文件中被使用
1.test.c
const int m_a = 1000;
2.main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
extern const int m_a;
printf("%d\n", m_a);
system("pause");
return EXIT_SUCCESS;
}
二、C++下const修饰全局变量默认是内部链接属性
在C++下 const 默认是内部链接属性,所以只能本文件中使用;
要想在其他文件中使用,可以使用 extern 关键字来提高作用域
1.test.cpp
const int m_b = 1005;
extern const int m_c = 1005;
2.main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main()
{
extern const int m_c;
cout << "m_c:" << m_c << endl;
system("pause");
return EXIT_SUCCESS;
}