home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / cplus / 1159 < prev    next >
Encoding:
Text File  |  1992-09-11  |  5.6 KB  |  209 lines

  1. Newsgroups: comp.std.c++,comp.lanf.c++
  2. Path: sparky!uunet!ftpbox!motsrd!news
  3. From: shang@corp.mot.com (David (Lujun) Shang)
  4. Subject: Proposal: Semantic Change on Operator "::"
  5. Message-ID: <1992Sep11.173823.17289@cadsun.corp.mot.com>
  6. Sender: news@cadsun.corp.mot.com
  7. Reply-To: shang@corp.mot.com
  8. Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
  9. Distribution: World
  10. Date: Fri, 11 Sep 92 17:38:23 GMT
  11. Lines: 196
  12.  
  13.  
  14.  
  15. [Purpose] 
  16.  
  17. Eliminate the ambiguity of the operator "::" currently used in C++.
  18.  
  19. [The Existing Problem]
  20.  
  21. Two kinds of meaning are associated with operator "::" in the current C++:
  22.  
  23.  
  24. (1) "::" as a selection operator:
  25.     class::name   // name is a component of the class.
  26.  
  27.     This expression is commonly used in accessing a nested class and 
  28.     a static member.
  29.  
  30.     Example1:
  31.        class A { public: static int i; };
  32.        int i = A::i;
  33.     Note that a static member can also be accessed through an object,
  34.     Therefore, "i" can be viewed as a component both of a class and of
  35.     an instance of this class.
  36.  
  37.     Example2:
  38.        class A { public: class B {}; };
  39.        A::B  ab;
  40.     where "A::B" means that "B" is a component class of class "A".
  41.  
  42. (2) "::" as a scope resolution operator:
  43.     ::name    // name is defined in global scope
  44.     class::name   // name is defined in this class or its super
  45.  
  46.     Example1:
  47.        class A { public: int i; };
  48.        class B: public A { public: int i; };
  49.        B b;
  50.        b.A::i;
  51.     where "A::i" suggests that "i" is defined in class "A", 
  52.     but "i" is not the component of "A", instead, it is the component 
  53.     of object "b". Therefore, in expression "b.A::i", the selector of 
  54.     "i" is "b" and "A" is only the scope resolute of "i".
  55.  
  56.     Example2:
  57.        class A { public: void foo (); };
  58.        void A::foo () {...};
  59.     where "A::foo" suggest "foo" is defined in class "A" ( Note that
  60.     "foo" is not the compnent of class "A", it belongs to an instance
  61.     of class A).
  62.  
  63. The problem is that we can not always distinguish the real role of operator  
  64. "::" in an expression.
  65.  
  66. [Ambiguity Examples]
  67.  
  68.         class A { public: static int i; };
  69.         class B: public A { public: static A A;   };
  70.  
  71.     What is the meaning of the expression "B::A::i"? If we consider 
  72.     the first "::" as the scope resolutor, "B::A::i" means the interger 
  73.     "i" defined in the base class. But if we consider the first "::" as 
  74.     a selector, the expression denotes the integer defined the component 
  75.     object A which is defined in class B.
  76.  
  77.     Thoung in fact the integer "i" is unique in this case, but how about
  78.     the following example:
  79.  
  80.         class A { public: static int i; };
  81.         class A_ { public: static int i; };
  82.         class B: public A { public: static A_ A;   };
  83.  
  84.     Now "B::A::i=100" will have different effect if we explain "::" in 
  85.     different meaning. Further, if we consider expression:
  86.  
  87.         B b;
  88.         b.A::i =100;
  89.  
  90.     Which "i" should get the value "100"? It depends on whether "::" is 
  91.     a selector or a scope resolute.
  92.        
  93. Example2
  94.  
  95.     class A { public: class NA {}; };
  96.     class B: public A 
  97.     { public:
  98.         class A { public: class NA{}; }; 
  99.     };
  100.  
  101.     What is the meaning of the expression "B::A::NA"?
  102.  
  103. We can set up a rule to avoid the ambiguity: whenever the ambiguity 
  104. happens, we apply the scope resolute meaning to the operator "::". (In
  105. fact, the langauge implementation has already apply this rule to solve 
  106. the ambiguity problem.). Therefore, "b.A::i" is the interger defined
  107. in class A, not the interger of the component A, i.e. the integer of 
  108. class A_.
  109.  
  110. But how about if the user wants the selector meaning rather than the scope  
  111. resolute? For example, in expression "B::A::NA", I actually mean the class NA  
  112. nexted in class A nested in class B, not the NA nested in global class A. And  
  113. unfortuenately, this the common case. ( And the langage implementation apply  
  114. the reverse rule for this case:  whenever the ambiguity happens, we apply the  
  115. selector meaning to the operator "::".)
  116.  
  117. What a chaos!
  118.  
  119. [Solution]
  120.  
  121. We use "::" only as a scope resolute. Whenever a selector must be used, use  
  122. operator ".". Then we come to a clear solution.
  123.  
  124. In example:
  125.  
  126.         class A { public: static int i; };
  127.         class A_ { public: static int i; };
  128.         class B: public A { public: static A_ A;   };
  129.         B b;
  130.  
  131. "B.A::i" denotes the integer of A_;
  132. "B::A.i" denotes the integer of A;
  133.  
  134. Note that a static member is also a member of the object, therefer,
  135.  
  136. "b.A::i" denotes the integer of A;
  137. "b.A.i" denotes the integer of A_;
  138.  
  139. In example:
  140.  
  141.     class A { public: class NA {}; };
  142.     class B: public A 
  143.     { public:
  144.         class A { public: class NA{}; }; 
  145.     };
  146.  
  147. "B.A::NA" denotes the NA nested in global class A (i.e. A.NA)
  148. "B.A.NA" denotes the NA nested in nested class A in B;
  149.  
  150. Nested class cannot be accessed through objects.
  151.  
  152. [Syntatic Modification]
  153.  
  154. qualified-name
  155.     constrained-name
  156.     constrained-name . qualified-name
  157.  
  158. constrained-name
  159.     :: simple-name
  160.     simple-name :: constrained-name
  161.  
  162. simple-name
  163.     identifier
  164.     (qualified-name)
  165.  
  166. Note that simple-name can be a parenthesized qualified name. Thus we can have  
  167. the expression like:
  168.  
  169.      C.NC.(BC1.NBC)::BBC1::NBBC
  170.  
  171. to denote a nested class NBBC in the following class definitions:
  172.  
  173.      class BBC1
  174.      { public:
  175.          class NBBC {};
  176.      };
  177.      class BBC2
  178.      { public:
  179.          class NBBC {};
  180.      };
  181.      class BC1
  182.      { public: 
  183.          class NBC: public BBC1, public BBC2 {};
  184.      };
  185.      class BC2
  186.      { public: 
  187.          class NBC: public BBC1, public BBC2 {};
  188.      };
  189.       class C
  190.      {  public:
  191.           class NC: public BC1.NBC, public BC2.NBC {};
  192.      };
  193.  
  194.  
  195. David Shang
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.