Overload new, new[], delete, and delete[] for the three_d class. : New Delete « 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 » New DeleteScreenshots 
Overload new, new[], delete, and delete[] for the three_d class.
  
#include <iostream>
#include <cstdlib>
#include <new>

using namespace std;

class three_d {
  int x, y, z; // 3-D coordinates
public:
  three_d() { x = y = z = 0}
  three_d(int i, int j, int k) { x = i; y = j; z = k; }

  // Set the coordinates of an object after it is created.
  void set(int i, int j, int k) { x = i; y = j; z = k; }

  // Overload new and delete for three_d objects.
  void *operator new(size_t size);
  void operator delete(void *p);

  // Overload new[] and delete[] for three_d arrays.
  void *operator new[](size_t size);
  void operator delete[](void *p);

  // Let the overloaded inserter be a friend.
  friend ostream &operator<<(ostream &strm, three_d op);
};

// The three_d inserter is a non-member operator function.
ostream &operator<<(ostream &strm, three_d op) {
  strm << op.x << ", " << op.y << ", " << op.z << endl;

  return strm;
}

// Overload new for three_d.
void *three_d::operator new(size_t size)
{
  void *p;

  cout << "Using overloaded new for three_d.\n";
  p = malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}

// Overload delete for three_d.
void three_d::operator delete(void *p)
{
  cout << "Using overloaded delete for three_d.\n";
  free(p);
}

// Overload new[] for three_d arrays.
void *three_d::operator new[](size_t size)
{
  void *p;

  cout << "Using overloaded new[] for three_d.\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}

// Overload delete[] for three_d arrays.
void three_d::operator delete[](void *p)
{
  cout << "Using overloaded delete[] for three_d.\n";
  free(p);
}

int main()
{
  three_d *p1, *p2;
  int i;

  // Allocate a three_d object.
  try {
    p1 = new three_d (102030);
  catch (bad_alloc xa) {
    cout << "Allocation error for p1.\n";
    return 1;
  }

  delete p1;

  try {
    p2 = new three_d [10]
  catch (bad_alloc xa) {
    cout << "Allocation error for p2.\n";
    return 1;
  }

  // Assign coordinates to three of p2's elements.
  p2[1].set(987);
  p2[5].set(-1, -2, -3);
  p2[8].set(678);

  cout << "Contents of a dynamic three_d array:\n";
  for(i=0; i<10; i++cout << p2[i];

  delete [] p2;

  return 0;
}
  
    
  
Related examples in the same category
1.Demonstrate overloaded new and delete.Demonstrate overloaded new and delete.
2.Define new.delete/new[]/delete[] operators
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.