Topic: [Just a simple question] (Iam i correct ?)


Author: Kannan Chellappan <chellapk@nmasd.bel.alcatel.be>
Date: 1998/07/29
Raw View
Siemel Naran wrote:

> [edited a little]
> >> struct Outer {
> >>
> >>   struct Inter {
> >>     struct Inner { int i; };
> >>   };
> >>
> >>   struct Other : public Inter::Inner { };
> >>
> >> };
>
> Consider declaring classes out of line as this is good for
> readability.
>
> >> int
> >> f (Outer::Other *op)
> >> {
> >>   op -> Inter::Inner::i = 0;
> >> }
>
> >I dont see any problem. The structure Inner can be accessed only
through
> >Inter (as Inner is in the scope of Inter) and the structures Other
and Inter
> >are in the same scope (that of Outer).
>
> If "Outer" were a namespace, then since the declaration of the
> function f(...) uses the namespace "Outer", the compiler pulls
> in this namespace.  Then
>      Inter::Inner::i
> refers to
>      Outer::Inter::Inner::i.
>
> But "Outer" is a class, so does it get pulled in?  I don't think so,
> because if it did get pulled in, the code below would compile.
>

Let us consider a very simple example before i try to explain why i
think
so...(without going into your example), let us consider this case :

class A{
public:
int fn1();
int fn2();

//some variables
float f;
...
...

};

int main()
{
  A a;
  A *ptr = &a;
  a.fn1();  // statements in main, not in member fns
  ptr->f = 10.5;
  return 0;
}

The above statements are legal. But it can be noted that the function
fn1() and
the variable f are in the scope of the class A. It should be possible
for me to
explicitly scope resolve it by saying something like
   a.A::fn1() ;
   or
  ptr->A::f = 10.5;
But since Iam accessing it with the concerned object (a) or pointer
(ptr) which
are of type A, i have a feeling that the complier does the necessary
scope
resolution as A::
or can i put it like this " the object "a" or the pointer "ptr" brings
with
itself the scope resolution A:: "


Back to the original problem, Inter and Other are in the scope of the
class
Outer. What is being passed is a pointer to an object of type Other
(which is in

the scope of Outer).  So i think, the scope resolution Outer:: is done
by the
presence of the pointer (op) in this case.
Hence without saying op->Outer::Inter::Inner::i = 10;, i am able to say
op->Inter::Inner::i = 10;

> void f(std::list<int>::iterator iter)
> // this pulls in namespace std
> // suppose it also pulls in std::list<int>
> {
>      value_type integer; // fine!
>      // value_type ==> std::list<int>::value_type ==> int
> }
>

I cannot do something like this

void f( Outer::Inter::Inner inn1)
{
  Inner inn; // Inner not in  scope
  ...
  // but i can say
  inn1.i = 10;
 }

Do you agree ? or am I wrong ?

> So back to the original example, the 'namespace' Outer (which is
> really a class) would only get pulled in function f(...) were
> a member or static function of class Outer, or a member or static
> function of one of the nested classes of class Outer (i.e. a member
> or static of Outer::Inter or Outer::Inter::Inner).
>
> Here's my correction.  Note that in this case, the scope resolution
> is not really necessary.
>
> >f (Outer::Other *op)
> >{
> >    op -> Inter::Inner::i = 0;
>
>      op -> Outer::Inter::Inner::i = 0;
>
> >Did you face any problems ?
>
> The original code as written does not compile on g++ 2.7.2.3.

I got is compiled on HP C++/HP-UX Version A.10.22   :-)

or is it compiler dependent ?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]