Variable
What is variable? (idiom: define variable where they are used)
Name for Object that is a region of memory with certain type.
What is reference?
int val1=10;
int &val2=val1; // can only feed reference with a variable name defined as the same type
int &val3=10; // error
int val1=10;
int &val2=val1;
val1+=2;
val2+=4;
cout<< val1<<" "<< val2;
16 16
Reference VS Pointer(Idiom:reference is not pointer)
Typedef
typedef double wages;
wages hourly=10,weekly=100;
Application on multidimensioned array
int ia[3][4]={{0},{1},{2}};//type 'int (*)[4]'
typedef int int_array[4];//really means: typedef int[4] int_array(not syntatically allowed )
//int_array *ip=ia;
for(int_array *p=ia;p!=ia+3;p++){
for(int *q=*p;q!=*p+4;q++){
cout<<*q;
}
}
Lvalue & Rvalue
Declaration (Multiple times) & Definition(Once)
Declaration (idiom : standing at the position of compiler)
enables the use of program fragments ,even when they are out of the scope. what is scope?
#include <iostream>
using namespace std;
extern int val2;
int main() {
int val=1.01;
int &rval1=val;
std::cout << val<< " " << rval1 <<" " << val2 << std::endl;
return 0;
val2 is not in the scope of test.cpp
int val2 =20;
Can we declare a variable which is never defined in program to cheat with compiler? The answer is no. We can only declare variable that is already defined somewhere in Program.
Forward-declaration & Forward-reference
void printThisInteger(int)
In C/ C++, the line above represents forward declaration of a function and is the function's prototype. After processing this declaration, the compiler would allow the programmer to refer to the entity
printThisInteger
in the rest of the program. Definition for a function must be provided somewhere (same file or other, where it would be responsibility of the linker to correctly match references to particular function in one or several object files with its definition, which must be unique, in another):
The term forward reference is sometimes used as a synonym of forward declaration.However, more often it is taken to refer to the actual use of an entity before any declaration; that is, the first reference to second
in the code above is a forward reference.[2][3] Thus, we may say that because forward declarations are mandatory in Pascal, forward references are prohibited.
An example of (valid) forward reference in C++:
class C { public: void mutator(int x) { myValue = x; } int accessor() { return myValue; } private: int myValue; };
In this example, there are two references to myValue
before it is declared. C++ generally prohibits forward references, but they are allowed in the special case of class members. Since the member function accessor
cannot be compiled until the compiler knows the type of the member variable myValue
, it is the compiler's responsibility to remember the definition of accessor
until it sees myValue
's declaration.
Permitting forward references can greatly increase the complexity and memory requirements of a compiler, and generally prevents the compiler from being implemented in one pass.
Initialization
Initialization & Assignment
int a[]; // this is a definition
you will get an error: error: storage size of 'a' isn't known.
int a; // a definition, storage allocated
result from my machine is: 4200942