Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Last active November 3, 2015 11:10
Show Gist options
  • Save bzdgn/bfa31e2f508763063a68 to your computer and use it in GitHub Desktop.
Save bzdgn/bfa31e2f508763063a68 to your computer and use it in GitHub Desktop.
Const Pointer Demo and Notes
// Const.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Person
{
private:
string firstName;
string lastName;
int arbitraryNumber;
public:
Person(string n, string l, int num) : firstName(n), lastName(l), arbitraryNumber(num){}
~Person(){}
string GetName() const { return firstName + " " + lastName + " "; }
int GetNumber() const { return arbitraryNumber; }
void SetNumber(int in) { arbitraryNumber = in; }
bool operator<(const Person &p) const{ return arbitraryNumber < p.arbitraryNumber; }
bool operator<(int i) const { return arbitraryNumber < i; }
friend bool operator<(int i, const Person &p);
};
bool operator<(int i, const Person &p) { return i < p.arbitraryNumber; }
int DoubleIt(const int &x)
{
return x * 2;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 3;
const int ci = 3;
i = 4;
//ci = 4
int j = 10;
int DoubleJ = DoubleIt(j);
int doubleTen = DoubleIt(10);
Person Kate("Kate", "Gregory", 234);
Kate.SetNumber(235);
const Person cKate = Kate;
//cKate.SetNumber(236);
int KateNumber = cKate.GetNumber();
int* pI = &i;
//int *pII = &ci;
const int *pII = &ci;
Person *pKat = &Kate;
Person Someone("Someone", "Else", 345);
const int *pcI = pI; // pointer to CONSTANT VALUE
//*pcI = 4;
pcI = &j;
int * const cpI = pI; // const pointer :CONSTANT POINTER
*cpI = 4;
//cpI = &j;
const int * const madPointer = pI;
//madPointer = &j; // pointer cant be changed
//*madPointer = 234; // content cant be changed
int k = *madPointer;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment