Demonstrate a very simple safe pointer class. : template class « Class « 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++ » Class » template classScreenshots 
Demonstrate a very simple safe pointer class.
  
#include <iostream>
#include <string>

using namespace std;

// The exception type thrown by the safe pointer.
class bad_ptr {
public:
  string msg;

  bad_ptr(string str) { msg = str; }
};

// A class used to demonstrate the safe pointer.
class myclass {
public:
  int alpha;
  int beta;
  myclass(int p, int q) { alpha = p; beta = q; }
};

template <class T> class safe_ptr {
  T *ptr;
public:
  safe_ptr() { ptr = 0}

  T *operator->() {
    if(!ptr != 0throw bad_ptr("Attempt to use -> on null pointer.");
    else return ptr;
  }

  T &operator*() {
    if(!ptrthrow bad_ptr("Attempt to dereference null pointer.");
    else return *ptr;
  }

  operator T *() { return ptr; }

  T *operator=(T *val) { ptr = val; return ptr; }
};

int main()
{
  safe_ptr<int> intptr;

  try {
    *intptr = 23;
    cout << "The value pointed to by intptr is: " << *intptr << endl;
  catch(bad_ptr bp) {
    cout << bp.msg << endl;
  }

  intptr = new int;

  try {
    *intptr = 23;
    cout << "The value pointed to by intpr is: " << *intptr << "\n\n";
  catch(bad_ptr bp) {
    cout << bp.msg << endl;
  }

  safe_ptr<myclass> mcptr;

  try {
    mcptr = new myclass(100200);
    cout << "The values of alpha and beta for mcptr are: "
         << mcptr->alpha << " and " << mcptr->beta << endl;

    mcptr->alpha = 27;
    cout << "New value for mcptr->alpha: " << mcptr->alpha << endl;
    cout << "Same as (*mcptr).alpha: " << (*mcptr).alpha << endl;

    mcptr->beta = 99;
    cout << "New value for mcptr->beta: " << mcptr->beta << "\n\n";
  catch(bad_ptr bp) {
    cout << bp.msg << endl;
  }

  safe_ptr<myclass> mcptr2;

  try {
    mcptr2->alpha = 88;
  catch(bad_ptr bp) {
    cout << bp.msg << endl;
  }

  delete intptr;
  delete mcptr;

  return 0;
}
  
    
  
Related examples in the same category
1.template class with type parameter
2.template class with generic parameter
3.A generic safe-array class that prevents array boundary errors.
4.Get storage off stack for array
5.template extending
6.sequence template
7.Template Version of Generic binary sorted Tree.
8.template class with two generic parameters
9.generic stack template class
10.Using exceptions with templates.
11.Passing by reference and using virtual functions in exceptions.
12.Using Non-Type Arguments with Generic Classes
13.Using Default Arguments with Template Classes
14.Generic Classes: demonstrates a generic stack.
15.An Example with Two Generic Data Types
16.Applying Template Classes: A Generic Array Class
17.Explicit Class Specializations for generic template 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.