// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>

using namespace std;

class A;

class B
{
    private:
        A *a;
    public:
        B(A *a);
        void foobar();
};

class A
{
    private:
        B *b;
    public:
        int v;
        A(int v = 0);
        void foobar();
};

B::B(A *a)
{
    this->a = a; // argument shadows for instance member
}

void B::foobar()
{
    cout << a->v << endl;
}

A::A(int v)
{
    this->v = v; // argument shadows for instance member
    b = new B(this); // pass itself as argument
}

void A::foobar()
{
    b->foobar();
}

int main()
{
    A *a1 = new A(123);
    a1->foobar();
    A *a2 = new A();
    a2->foobar();
    return 0;
}
