C++/Pointer/Pointer Primitive — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:21, 25 мая 2010

Assign the public object member address to a pointer

 
#include <iostream>
using namespace std;
class MyClass {
public:
  int i;
  MyClass(int j) { 
     i = j; 
  }
};
int main()
{
  MyClass myObject(1);
  int *p;
  p = &myObject.i;                     
  cout << *p;                          
  return 0;
}


Declares a pointer and then outputs its value without first assigning

 
#include <iostream>
using namespace std;
int main ()
{
   int* intPointer;
   cout << "The value of intPointer is " << intPointer << endl;
   return 0;
}


Decrementing a Pointer for int value

 
#include <iostream>
using namespace std;
const int Length = 3;
int main ()
{
   int testScore[Length] = {4, 7, 1};
   int* intPointer = &testScore[Length - 1];
   int i = Length - 1;
   while (intPointer >= &testScore[0])
   {
      cout << "The address of index " << i
         << " of the array is "<< intPointer << endl;
      cout << "The value at index " << i
         << " of the array is "<< *intPointer << endl;
      intPointer--;
      i--;
   }
   return 0;
}


How to use the address operator to assign the address of a variable to a pointer

 

#include <iostream>
using namespace std;
int main ()
{
   int num = 5;
   int* intPointer = &num;
   cout << "The address of x using &num is " << &num << endl;
   cout << "The address of x using intPointer is " << intPointer << endl;
   return 0;
}


Indirection Operator and Dereferencing

 
#include <iostream>
using namespace std;
int main ()
{
   int num = 5;
   int* intPointer = &num;
   cout << "The value of num is " << num << endl;
   num = 10;
   cout << "The value of num after num = 10 is " << num << endl;
   *intPointer = 15;
   cout << "The value of num after *intPointer = 15 is " << num << endl;
   return 0;
}


Null Pointers

 
#include <iostream>
using namespace std;
int main ()
{
   int* intPointer;
   intPointer = NULL;
   cout << "The value of intPointer is " << intPointer << endl;
   return 0;
}


Pointer as a Variable

 
#include <iostream>
using namespace std;
int main ()
{
   int num1 = 5, num2 = 14;
   int* intPointer = &num1;
   cout << "The value of num1 is " << num1 << endl;
   *intPointer *= 2;
   cout << "The value of num1 after *intPointer *= 2 is " << *intPointer << endl;
   intPointer = &num2;
   
   cout << "The value of num2 is " << num2 << endl;
   
   *intPointer /= 2;
   cout << "The value of num after *intPointer /= 2 is " << *intPointer << endl;
   return 0;
}


Pointers to Class Members

 
#include <iostream>
using namespace std;
class MyClass {
public:
  MyClass(int i) { 
     val=i; 
  }
  int val;
  int doubleValue() { 
     return val+val; 
  }
};
int main()
{
  int MyClass::*data;                                 
  int (MyClass::*func)();                             
  MyClass myObject1(1), myObject2(2);                 
  data = &MyClass::val;                               
  func = &MyClass::doubleValue;
  cout << "Here are values: ";
  cout << myObject1.*data << " " << myObject2.*data << "\n";
  cout << "Here they are doubled: ";
  cout << (myObject1.*func)() << " ";
  cout << (myObject2.*func)() << "\n";
  return 0;
}


Prints the values and addresses of variables.

 
#include <iostream>
using namespace std;
int var, *ptr;    
int main()        
{                 
   var = 100;
   ptr = &var;
   cout << " Value of var:      " <<  var << "   Address of var: " <<  &var
        << endl;
   cout << " Value of ptr: "      <<  ptr << "   Address of ptr: " <<  &ptr
        << endl;
   return 0;
}


Returning a pointer

  
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);
int main()
{
    vector<string> v;
    v.push_back("A");
    v.push_back("B");
    v.push_back("C");
     cout << *(ptrToElement(&v, 0)) << endl;
     string* pStr = ptrToElement(&v, 1);
     cout << *pStr << endl;
    
     string str = *(ptrToElement(&v, 2));  
     cout << str << endl;
    
     *pStr = "Healing Potion";
     cout << v[1] << endl;
    
    return 0;
}
string* ptrToElement(vector<string>* const pVec, int i)
{
    //returns address of the string in position i of vector that pVec points to
    return &((*pVec)[i]);  
}


The actual data type of the value of all pointers is the same

 

#include <iostream>
using namespace std;
int main ()
{
   int* intPointer;
   float* floatPointer;
   char *charPointer;
   cout << "The size of intPointer is " << sizeof(intPointer) << endl;
   cout << "The size of floatPointer is " << sizeof(floatPointer) << endl;
   cout << "The size of charPointer is " << sizeof(charPointer) << endl;
   return 0;
}


Using a reference parameter for class type

 
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class StringClass {
  char *p;
public:
  StringClass(char *s);
  ~StringClass() { 
     delete [] p; 
  }
  char *get() { 
     return p; 
  }
};
StringClass::StringClass(char *s)
{
  int l;
  l = strlen(s)+1;
  p = new char [l];
  if(!p) {
    cout << "Allocation error\n";
    exit(1);
  }
  strcpy(p, s);
}
// Using a reference parameter.
void show(StringClass &x)
{
  char *s;
   
  s = x.get();
  cout << s << endl;
}
int main()
{
  StringClass stringObject1("Hello"), stringObject2("There");
  show(stringObject1);
  show(stringObject2);
  return 0;
}