Scope
- Scope, determine where a variable is accessible
- Duration, determine where it is created and destroyed
Storage Class
- Define the scope and life-time of variables and/pr functions within a C++ program
- auto, default storage class for all local variables, can only be used within functions
- register, was designed to save variables in a register instead of memory, to most of compiler, regiser has the same semantics a auto
- static, keep a local variable existing during the life-time of the program, cause a global variable to be restricted to the file in which it is declared
- extern, give a reference of a global variable that is visible to all the program files, to tell the compiler the global variable or function has been defined in another file
- mutable, make a data member of class to be modifiable through const function
//static
#include <iostream>
static int count = 0;
void increase()
{
static int index = 0;
index++;
count++;
std::cout<<index<<" "<<count<<std::endl;
}
int main(int argc, char *argv[])
{
increase();
increase();
return 0;
}
//util.h
#ifndef UTIL_H
#define UTIL_H
extern const float pi;
float getArea(int r)
{
return pi*r*r;
}
#endif
//main.cpp
#include <iostream>
#include "util.h"
extern const float pi = 3.14;
int main(int argc, char *argv[])
{
std::cout<<pi<<std::endl;
std::cout<<getArea(10)<<std::endl;
return 0;
}
#include <iostream>
#include <string>
class Customer
{
private:
mutable std::string name;
public:
Customer(std::string n):name(n){}
void changeName(std::string n) const
{
name = n;
}
std::string getName() const {return name;}
};
int main(int argc, char *argv[])
{
Customer c("Lin Chen");
c.changeName("Yanhua Feng");
std::cout<<c.getName()<<std::endl;
return 0;
}
Local Scope
#include <iostream>
int main(int argc, char *argv[])
{
int a = 0;
{
int b = 10;
std::cout<<a<<" "<<b<<std::endl;
}
std::cout<<a<<std::endl;
//std::cout<<b<<std::endl;//error
return 0;
}
- Local variable, defined inside a block, have automatic duration, which means they are created at the point of definition, and destroyed when the block are define in is exited
- variable defined outer block can be seen inside a nested block
Shadowing
#include <iostream>
int main(int argc, char *argv[])
{
int a = 0;
{
int a = 10;
std::cout<<a<<std::endl;
}
std::cout<<a<<std::endl;
return 0;
}
- A variable inside a nested block have the same name as a variable inside a ouiter block
- Variables should be defined in the most limited scope possible
Global Scope
#include <iostream>
int count = 0;
void increase()
{
count++;
std::cout<<"Global variable: "<<count<<std::endl;
}
int main(int argc, char *argv[])
{
increase();
int count = 10;//shadow the global variable
std::cout<<"Local variable: "<<count<<std::endl;
std::cout<<"Global variable: "<<::count<<std::endl;
return 0;
}
- static duration, created when the program starts and are destroyed when it ends
- file scope, visible until the end of the file in which they are declared
- Variable in an inner block with the same name as a global varialbe shadow the global variable
- Global variable can be accessed by the global scope operator (::)
Function Linkage
Function always default to external linkage, can be set to internal linkage via the static keyword
File Scope and Global Scope
- All global variables in C++ have file scope
- File scope is applied to file scope varaiable with internal linkage only
- Global scope is applied to file scope variables with external linkage
Reference