this pointer c++ code example
Example 1: c++ pointers
#include <iostream>
using namespace std;
int main () {
int var = 20;
int *ip;
ip = &var;
cout << "Value of var variable: ";
cout << var << endl;
cout << "Address stored in ip variable: ";
cout << ip << endl;
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
Example 2: c++ pointers
#include <iostream>
using namespace std;
int main()
{
double* account_pointer = new double;
*account_pointer = 1000;
cout << "Allocated one new variable containing " << *account_pointer
<< endl;
cout << endl;
int n = 10;
double* account_array = new double[n];
for (int i = 0; i < n; i++)
{
account_array[i] = 1000 * i;
}
cout << "Allocated an array of size " << n << endl;
for (int i = 0; i < n; i++)
{
cout << i << ": " << account_array[i] << endl;
}
cout << endl;
double* bigger_array = new double[2 * n];
for (int i = 0; i < n; i++)
{
bigger_array[i] = account_array[i];
}
delete[] account_array;
account_array = bigger_array;
n = 2 * n;
cout << "Now there is room for an additional element:" << endl;
account_array[10] = 10000;
cout << 10 << ": " << account_array[10] << endl;
delete account_pointer;
delete[] account_array;
return 0;
}
Example 3: this in c++
#include <iostream>
class Entity
{
public:
int x, y;
Entity(int x, int y)
{
Entity*const e = this;
this->x = x;
this->y = x;
}
int GetX()const
{
const Entity* e = this;
}
};
int main()
{
Entity e1(1,2);
}
Example 4: pointers c++
baz = *foo;