Operator overload: new, delete, new[] and delete[] : Index « Overload « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Overload » IndexScreenshots 
Operator overload: new, delete, new[] and delete[]
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 (1020)
  
  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();
  
  fori = 0; i < 10; i++)
    objectPointer2[i].show();

  delete objectPointer1;                             // free an object
  delete [] objectPointer2;                          // free an array

  return 0;
}


           
         
  
Related examples in the same category
1.Define operator []Define operator []
2.Overload [].Overload [].
3.Return a reference from [].Return a reference from [].
4.String class: Index characters and '=' operatorString class: Index characters and '=' operator
5.Index operator for class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.