码迷,mamicode.com
首页 > 其他好文 > 详细

nested friend

时间:2016-03-24 14:39:03      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

const int sz=20;

struct Holder{
private:
    int a[sz];
public:
    void initialize();
    struct Pointer;
    friend Pointer;
    struct Pointer{
    private:
        Holder *h;
        int *p;
    public:
        void initialize(Holder *h);
        void next();
        void previous();
        void top();
        void end();
        int read();
        void set(int i);
    };
};

void Holder::initialize(){
    memset(a,0,sz*sizeof(int));
}

void Holder::Pointer::initialize(Holder *rv){
    h=rv;
    p=rv->a;
}

void Holder::Pointer::next(){
    if(p<&(h->a[sz-1]))p++;
}

void Holder::Pointer::previous(){
    if(p>&(h->a[0]))p--;
}

void Holder::Pointer::top(){
    p= &(h->a[0]);
}

void Holder::Pointer::end(){
    p= &(h->a[sz-1]);
}

int Holder::Pointer::read(){
    return *p;
}

void Holder::Pointer::set(int i){
    *p = i;
}

int _tmain(int argc, _TCHAR* argv[])
{
    Holder h;
    Holder::Pointer hp,hp2;
    int i;
    h.initialize();
    hp.initialize(&h);
    hp2.initialize(&h);
    for(i=0;i<sz;i++){
        hp.set(i);
        hp.next();
    }
    hp.top();
    hp2.end();
    for(i=0;i<sz;i++){
        cout<<hp.read()<<"  "<<hp2.read()<<endl;
        hp.next();
        hp2.previous();
    }
    return 0;
}

技术分享

nested friend

标签:

原文地址:http://www.cnblogs.com/kateblog/p/5315371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!