Contents
- Tokens
- New & Delete
- const
- conversion
- reference
1. Tokens
The smallest individual units in a program.
C++ has the following tokens:
- keywords
- identifiers
- constants
- strings
- operators
Note 1
C++ requires a const to be initialized.
const int a; //error
const int a = 5; //correct
Note 2
A const in C++ is local to the file where it is declared.
To give a const value an external linkage, we must explicitly define it as an extern in C++.
There is an example:
I created 3 files.
// mian.cpp
#include <iostream>
#include "file1.hpp"
using namespace std;
int main()
{
extern int a;
cout<<a<<endl;
}
// file1.hpp
#ifndef file1_hpp
#define file1_hpp
#include <stdio.h>
#endif /* file1_hpp */
// file1.cpp
#include "file1.hpp"
int a = 2;
Output
2
2. New&Delete
Note 3
A data object created inside a block with new, will remain in existence until it is explicitly destroyed by using delete.
Note 4
Forms:
- pointer-variable = new data-type;
- pointer-variable = new data-type(x);
- pointer-variable = new data-type[n];
tips:
Here, x specifies the initial value.
n specifies the number of elements in the array.
multi-dimentional
When creacting multi-dimentional arrays with new, all the array size must be supplied.
legal:
- Ar_P = new int [1][2][3];
- Ar_P = new int [m][2][3];
illegal:
- Ar_P = new int [m][2][ ];
Note 5
Forms:
- delete [size] pointer-variable;
- delete [ ] pointer-variable;
the second one will delete the entire array
3. const
Note 6
Using const with pointers
6.1 Nonconstant pointer to constant data
Forms:
- const type *p
- type const *p
Example:
#include <iostream>
using namespace std;
int main()
{
int a = 1;
const int *p;
p = &a;
cout<<*p<<endl;
a = 2;
cout<<*p<<endl;
// *p = 3; //illegal
// cout<<*p<<endl;
}
6.2 constant pointer to nonconstant data
Form:
- type *const p
Example:
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 3;
int *const p = &a; //must be initialized when they are declared
cout<<*p<<endl;
*p = 2;
cout<<*p<<endl;
// p = &b; //illegal
// cout<<*p<<endl;
}
6.3 constant pointer to constant data
Forms:
- const type *const p
- const type const *p
Example:
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 3;
const int *const p = &a; //must be initialized when they are declared
cout<<*p<<endl;
// *p = 2; //illegal
cout<<*p<<endl;
// p = &b; //illegal
// cout<<*p<<endl;
}
Note 7
int *p1;
const int y = 9;
p1 = &y; //illegal
4. conversion
Note 8
Explicit conversion
(type) expression //c version
type (expression) //c++ version
ANSI C++ adds the following new cast operators
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast
general form: x_cast < type > (exp)
type: target type
8.1 static_cast
used for any standard conversion of data types.
It can realize all the cast that implicit conversions do.
int m=10;
double x;
x= static_cast<double>(m);
cout<<x<<endl;
8.2 const_cast
used for explicitly override or volatile in a cast.
the target type must be the same as the source type.
#include <iostream>
using namespace std;
int main()
{
int a =1;
const int *p;
p=&a;
cout<<*p<<endl;
int* tmp = const_cast<int*>(p);
*tmp=2;
cout<<*p<<endl;
}
#include <iostream>
using namespace std;
int main()
{
const int a =1;
cout<<a<<endl;
//int b = const_cast<int>(a); //illegal
//Const_cast to 'int', which is not a reference, pointer-to-object, or pointer-to-data-member
}
8.3 reinterpret_cast
used for change one type into a fundamentally different type.
#include <iostream>
using namespace std;
int main()
{
int a=1;
int *p = &a;
long long c = 0;
c = reinterpret_cast<long long>(p);
cout<<c<<endl;
}
output:
140732920755516
8.4 dynamic_cast
used for cast the type of an object at run time
Its main application is to perform casts on polymorphic objects.
The type must be a pointer or reference to a defined class type.
5. reference
Note 9
Form:
- data-type & reference-name = variable - name
tips: Reference must be initialized and it’s initial value must be an object that has memory address.
the following example is allowed:
m is a reference of x
int x;
int *p = &x;
int & m = *p;
Note 10
A major application of reference variables is in passing arguements to functions.