Private: 私有的:extern的用法

bigzhu posted @ Dec 10, 2007 06:05:00 PM in cpp , 1557 阅读

终于明白c 区分声明和定义是为了让同一个变量,让多个文件来访问.

声明只是为了告诉编译器:我要在这里使用这个变量,但是他的定义不在这里,你到include的文件里面去找吧.

//test2.cpp
int x = 100;
int y = 200;

然后在另外一个文件里面,使用这两个变量:

// test.cpp
// extern 範例
#include
#include “test2.cpp” // 定义 x 和 y
int a = 10;
extern int x; // 有效範圍; 整個程式
int main(int argc, char* argv[])
{
extern int y; // 有效範圍: 這個函數內
std::cout << a << std::endl; // 10
std::cout << x << std::endl; // 100
std::cout << y << std::endl; // 200
return 0;
}


NCERT Sanskrit Sampl 说:
Sep 29, 2022 01:21:48 PM

Download NCERT Sanskrit Sample Paper 2023 Class 9 gives the candidates a fair idea of new exam scheme or question pattern for all formats of the exam such as SA1, SA2, FA1, FA2, FA3, FA4 and Assignments. NCERT Sanskrit Sample Paper Class 9 These NCERT STD-9 Sanskrit Sample Paper 2023 were Designed based on the newly revised syllabus and curriculum in all formats of exams conductingin Term-1 & Term-2 of the course.Download NCERT Sanskrit Sample Paper 2023 Class 9 gives the candidates a fair idea of new exam scheme or question pattern for all formats of the exam such as SA1, SA2, FA1, FA2, FA3, FA4 and Assignments.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter