Topic: Circular definitions that can't be resolved


Author: ronen@smoggy.gg.caltech.edu (Ronen Barzel)
Date: Sat, 25 Jan 1992 21:59:34 GMT
Raw View
A recent article about circular definitions prompted me to post these
circularities which, as far as I can tell, require kludges to work around:


 WHAT I WANT TO DO             KLUDGE THAT WORKS
 -----------------             -----------------
 extern int B::x;              extern int &B_x;

 class A {                     class A {
     f(int = B::x);                f(int = B_x);
 };                            };

 class B {                     class B {
     A a;                          A a;
 public:                       public:
     static int x;                 static int x;
 };                            };

          // not in the include file
          int &B_x = B::x;


 WHAT I WANT TO DO             KLUDGE THAT WORKS
 -----------------             -----------------
 class R;                      class R;

 class Q {                     class Q {
     static R r;                  static R &r;
 };                            };

 class R {                     class R {
     Q q;                          Q q;
 };                            };

          // not in the include file
          static R Q_r;
          R &Q::r = Q_r;


The general problem here is that I want to put pretty much anything into a
particular scope.  Since C++ requires names for a given scope to be defined
syntactically within the { ... } of its class definition, one encounters
difficulties using or defining a name in a particular scope that is defined or
uses names from a later scope.

I often wish C++ had a more flexible scope mechanism, in which namespace
scoping is not tied so closely to class definitions.  While it is true that
one can find a workaround to most any snag, there's often an expense of
uglier definitions, extra little snippets of code to maintain, etc.---
Consider the kludging required if, in the first example above, B::x was
private, and A was a friend of B.

I'm clearly pining for something closer to Common LISP's "package" mechanism.
(There are many features of Common LISP/Common LOOPS that I miss dearly.
And of course, there are many things about C++ that I would miss were I to
retern to LISP.  I don't mean to start a religious war.)

Can anybody find a better way to do what I want?  (No "you shouldn't want it"
responses, please! :) Are any scoping enhancements in store for C++ >= 3.0?

-Ronen
ronen@gg.caltech.edu