C++/Overload/Index

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Define operator []

 
#include <iostream>
using namespace std;
class MyClass {
  int a[3];
public:
  MyClass(int i, int j, int k) {
    a[0] = i;
    a[1] = j;
    a[2] = k;
  }
  int operator[](int i) { 
     return a[i]; 
  }
};
int main()
{
  MyClass object(1, 2, 3);
  cout << object[1];
  return 0;
}


Index operator for class

  
#include <iostream>
#include <stdlib.h>
using namespace std;
class sometype {
  int a[3];
  public:
   sometype(int i, int j, int k)
    {
      a[0] = i;
      a[1] = j;
      a[2] = k;
    }
   int &operator[](int i);
};
int &sometype::operator[](int i)
{
   if (i<0 || i>2)
    {
      cout << "Boundary error.\n";
      exit(1);
    }
   return a[i];
}

int main(void)
{
   sometype ob(1, 2, 3);
   cout << ob[1];
   cout << endl;
   ob[1] = 25;
   cout << endl;
   cout << ob[1];
   ob[3] = 44;
}


Operator overload: new, delete, new[] and delete[]

 
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class MyClass {
  int x, y;
public:
  MyClass() {
     x = y = 0;
  }
  
  MyClass(int lg, int lt) {
    x = lg;
    y = lt;
  }
  void show() {
    cout << x << " ";
    cout << y << endl;
  }
  void *operator new(size_t size);
  void operator delete(void *p);
  void *operator new[](size_t size);
  void operator delete[](void *p);
};
// overloaded new operator
void *MyClass::operator new(size_t size)
{
  void *p;
  cout << "In overloaded new.\n";
  
  p =  malloc(size);
  
  if(!p) {
  
    bad_alloc ba;
  
    throw ba;
  }
  return p;
}
// delete operator overloaded
void MyClass::operator delete(void *p)
{
  
  cout << "In overloaded delete.\n";
  
  free(p);
}
// new operator overloaded for arrays.
void *MyClass::operator new[](size_t size)
{
  void *p;
  cout << "Using overload new[].\n";
  
  p =  malloc(size);
  
  if( !p ) {
  
    bad_alloc ba;
  
    throw ba;
  }
  
  return p;
}
// delete operator overloaded for arrays.
void MyClass::operator delete[](void *p)
{
  cout << "Freeing array using overloaded delete[]\n";
  
  free(p);
}
int main()
{
  MyClass *objectPointer1, *objectPointer2;
  int i;
  try {
  
    objectPointer1 = new MyClass (10, 20); 
  
  } catch (bad_alloc xa) {
  
    cout << "Allocation error for objectPointer1.\n";
  
    return 1;;
  }
  try {
  
  
    objectPointer2 = new MyClass [10];               // allocate an array
  
  } catch (bad_alloc xa) {
  
    cout << "Allocation error for objectPointer2.\n";
  
    return 1;;
  }
  objectPointer1->show();
  
  for( i = 0; i < 10; i++)
    objectPointer2[i].show();
  delete objectPointer1;                             // free an object
  delete [] objectPointer2;                          // free an array
  return 0;
}


Overload [].

 
#include <iostream>
using namespace std;
const int SIZE = 3;
class MyClass {
  int a[SIZE];
public:
  MyClass() {
    register int i;
    for(i = 0; i <SIZE; i++) 
       a[i] = i;
  }
  int operator[](int i) {
     return a[i];
     
  }
};
int main()
{
  MyClass myObject;
  cout << myObject[2];
  return 0;
}


Return a reference from [].

 
#include <iostream>
using namespace std;
const int SIZE = 3;
class MyClass {
  int a[SIZE];
public:
  MyClass() {
    register int i;
    for(i = 0; i <SIZE; i++) 
       a[i] = i;
  }
  int &operator[](int i) {
     return a[i];
  }
};
int main()
{
  MyClass myObject;
  cout << myObject[2];  
  cout << " ";
  myObject[2] = 25;     // [] on left of =
  cout << myObject[2];  // now displays 25
  return 0;
}


String class: Index characters and "=" operator

 
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class StringClass {
  char *p;
  int len;
public:
  StringClass(char *s);
  ~StringClass() { 
     cout << "Freeing " << (unsigned) p << "\n";
     delete [] p;
   }
  char *get() { return p; }
  StringClass &operator=(StringClass &ob);
  char &operator[](int i);
};
StringClass::StringClass(char *s)
{
  int l;
  l = strlen(s)+1;
  p = new char [l];
  if(!p) {
    cout << "Allocation error\n";
    exit(1);
  }
  len = l;
  strcpy(p, s);
}
// = operator
StringClass &StringClass::operator=(StringClass &ob)
{
  // see if more memory is needed
  if(len < ob.len) { // need to allocate more memory
    delete [] p;
    p = new char [ob.len];
    if(!p) {
      cout << "Allocation error\n";
      exit(1);
    }
  }
  len = ob.len;
  strcpy(p, ob.p);
  return *this;
}  
// 
char &StringClass::operator[](int i)
{
  if(i <0 || i>len-1) {
    cout << "\nIndex value of ";
    cout << i << " is out-of-bounds.\n";
    exit(1);
  }
  return p[ i ];
}
int main()
{
  StringClass a("Hello"), b("There");
  cout << a.get() << "\n";
  cout << b.get() << "\n";
  a = b; // now p is not overwritten
  cout << a.get() << "\n";
  cout << b.get() << "\n";
  cout << a[0] << a[1] << a[2] << endl;
  a[0] = "X";
  a[1] = "Y";
  a[2] = "Z";
  cout << a.get() << endl;
  return 0;
}