友元类的一种设计
#include <stdio.h>
#include <string.h>
#define SZ 20
struct holder{
private:
int a[SZ];
public:
void initialize();
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);
};
friend holder::pointer;
};
void holder::initialize()
{
memset(a, 0, sizeof(int) * SZ);
}
void holder::pointer::initialize(holder* H)
{
h = H;
p = h->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]);
}
void holder::pointer::set(int i)
{
*p = i;
}
int main()
{
return 0;
}