Variable

Variable

What is variable?  (idiom: define variable where they are used)

Name for Object that is  a region of memory with certain type.


By the time of compilation variable names are converted into address in memory.

What is reference?

Alias, alterntive name for a variable

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)

Taking the example above, val2 is a reference to val1, which means by the time of compilation val2 and val1 will be substituted with the same memory address.
So that,
1 It's an error to define a reference without initializing it.
2 Once initialized, a reference always refers to the same underlying object.
3 Any Assignments to reference has an equivalent effect on the variable referenced.(they manipulate the same memory)

Typedef

defines synonym for a certain variable

	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;
		}
	}


Challenge: How to express a 3-dimensional array



Lvalue & Rvalue

Rvalue can only appear on the right side of assignment, but lvalue can appear on both sides.


Declaration (Multiple times) & Definition(Once)

Definition is allocation of storage for variables by compilation. Variables which are not defined can not be used legally in programs. 

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?


test.ccp
#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

t2.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

In  computer programming , a  forward declaration  is a  declaration  of an  identifier  (denoting an entity such as a type, a variable, or a function) for which the programmer has not yet given a complete  definition . It is required for a compiler to know the type (size) of an identifier, but not a particular value it holds (in case of variables).
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 accessoruntil 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 


copy-direct initialization


Initialization & Assignment

The storage of variable is allocated at the time of initialization or definition? at the time of definition. The definition tells compiler there would be such
type of variable in following program and allocates a storage in memory.

Things seem to be little different on array. If you try to execute following statement
int a[];   // this is a definition

you will get an error: error: storage size of 'a' isn't known. 

If you try to use a global varialbe without initialization. C++ will give a undefined result

int a; // a definition, storage allocated

result from my machine is: 4200942




Scope




Problem






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值