C++类成员访问运算符

// Consider an actual class.
class Obj {
   static int i, j;

public:
   void f() const { cout << i++ << endl; }
   void g() const { cout << j++ << endl; }
};

// Static member definitions:
int Obj::i = 10;
int Obj::j = 12;

// Implement a container for the above class
class ObjContainer {
   vector<Obj*> a;

   public:
      void add(Obj* obj) { 
         a.push_back(obj);  // call vector's standard method.
      }
      friend class SmartPointer;
};

// implement smart pointer to access member of Obj class.
class SmartPointer {
   ObjContainer oc;
   int index;

   public:
      SmartPointer(ObjContainer& objc) { 
         oc = objc;
         index = 0;
      }

      // Return value indicates end of list:
      bool operator++() { // Prefix version 
         if(index >= oc.a.size()) return false;
         if(oc.a[++index] == 0) return false;
         return true;
      }

      bool operator++(int) { // Postfix version 
         return operator++();
      }

      // overload operator->
      Obj* operator->() const {
         if(!oc.a[index]) {
            cout << Zero value;
            return (Obj*)0;
         }

         return oc.a[index];
      }
};

int main() {
   const int sz = 10;
   Obj o[sz];
   ObjContainer oc;

   for(int i = 0; i < sz; i++) {
      oc.add(&o[i]);
   }

   SmartPointer sp(oc); // Create an iterator
   do {
      sp->f(); // smart pointer call
      sp->g();
   } while(sp++);

   return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


#include <iostream> using namespace std; int main () { int*ptr = NULL; cout << "The value of ptr is " << ptr ;
#include <iostream> using namespace std; int main () { intvar = 20;// actual variable declaration.
// #include <iostream> using namespace std; int main () { intvar1; char var2[10]; cout << "Address of var1 variable: ";
#include <iostream> #include <string> using namespace std; int main () { string str1 = "Hello";
#include <iostream> #include <cstring> using namespace std; int main () { char str1[10] = "Hello";
#include <iostream> using namespace std; int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\\0'};
#include <iostream> #include <ctime> using namespace std; // function to generate and retrun random numbers.
#include <iostream> using namespace std; double getAverage(int arr[], int size) { int i, sum = 0; double avg;
#include <iostream> using namespace std; int main () { // an array with 5 elements. double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
#include <iostream> using namespace std; int main () { // an array with 5 rows and 2 columns. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ];// n is an array of 10 integers
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () {
#include <iostream> #include <cmath> using namespace std; int main () { // number definition:
#include <iostream> using namespace std; int main () { // number definition: shorts; inti; longl; floatf;
#include <iostream> using namespace std; // function definition to swap the values. void swap(int &x, int &y) {
#include <iostream> using namespace std; // function definition to swap the values. void swap(int *x, int *y) {
#include <iostream> using namespace std; void swap(int x, int y) { int temp; temp = x; /* save the value of x */
#include <iostream> using namespace std; int sum(int a, int b = 20) { int result; result = a + b; return (result);
// 标准输入流 #include <iostream> using namespace std; int main() { char name[50]; cout << "Please enter your name: ";
// C++标准输出流 #include <iostream> using namespace std; int main() { char str[] = "Hello C++";