Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in C++
Prev Contents / Index Next

Nested friends

Making a structure nested doesn’t automatically give it access to private members. To accomplish this, you must follow a particular form: first, declare (without defining) the nested structure, then declare it as a friend, and finally define the structure. The structure definition must be separate from the friend declaration, otherwise it would be seen by the compiler as a non-member. Here’s an example:

//: C05:NestFriend.cpp
// Nested friends
#include <iostream>
#include <cstring> // memset()
using namespace std;
const int sz = 20;

struct Holder {
  int a[sz];
  void initialize();
  struct Pointer;
  friend struct Pointer;
  struct Pointer {
    Holder* h;
    int* p;
    void initialize(Holder* h);
    // Move around in the array:
    void next();
    void previous();
    void top();
    void end();
    // Access values:
    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 main() {
  Holder h;
  Holder::Pointer hp, hp2;
  int i;

  for(i = 0; i < sz; i++) {
  for(i = 0; i < sz; i++) {
    cout << "hp = " <<
         << ", hp2 = " << << endl;;
} ///:~

Once Pointer is declared, it is granted access to the private members of Holder by saying:

friend struct Pointer;

The struct Holder contains an array of ints and the Pointer allows you to access them. Because Pointer is strongly associated with Holder, it’s sensible to make it a member structure of Holder. But because Pointer is a separate class from Holder, you can make more than one of them in main( ) and use them to select different parts of the array. Pointer is a structure instead of a raw C pointer, so you can guarantee that it will always safely point inside the Holder.

The Standard C library function memset( ) (in <cstring>) is used for convenience in the program above. It sets all memory starting at a particular address (the first argument) to a particular value (the second argument) for n bytes past the starting address (n is the third argument). Of course, you could have simply used a loop to iterate through all the memory, but memset( ) is available, well-tested (so it’s less likely you’ll introduce an error), and probably more efficient than if you coded it by hand.

Thinking in C++
Prev Contents / Index Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire