Topic: scoping rules : public, protected, private


Author: Jeroen Dobbelaere H3 SH7XX X9999 <dobbelaj@sh.bel.alcatel.be>
Date: 1996/08/06
Raw View
Hello,

 here I have some questions about scoping rules with classes.
Following program is faulty and tries to check some compilers and there
scoping rules.  Some lines give different results on different
compilers. Can someone please tell what the standard would force and
why ?

Lines which I think that should give warnings/errors are assigned with
'myself'.  Lines that are found erroneous by 'Borland 3.1', 'Zortech 2.1'
and 'g++2.7.2' are also assigned with their name.

The two lines assigned with '***' need special attention.


class A memberfunction :
***1 : tries to access a protected member (inherited of class A) of an object
of class B
***2 : tries to access a private member (inherited of class A) of an object
of class B


Greetings,
 Rombauts Geert
 Dobbelaere Jeroen

---begin of faulty program---

//---------------------------------------------------------------------------

class B;

//---------------------------------------------------------------------------

class A
{
   public:

      void af ( void );

      A* ap1;
      A* ap2;
      B* ap3;

      int ad1;

   protected:

      int ad2;

   private:

      int ad3;

};

//---------------------------------------------------------------------------

class B: public A
{
   public:

      void bf ( void );

      A* bp1;
      A* bp2;
      B* bp3;

      int bd1;

   protected:

      int bd2;

   private:

      int bd3;

};

//---------------------------------------------------------------------------

A a, a1, a2, a3;
B b, b1, b2, b3, b4, b5, b6;

int main ( void );

//---------------------------------------------------------------------------

int main ( void )
{
   a.ap1 = &a1;
   a.ap2 = &b1;
   a.ap3 = &b2;

   b.ap1 = &a2;
   b.ap2 = &b3;
   b.ap3 = &b4;
   b.bp1 = &a3;
   b.bp2 = &b5;
   b.bp3 = &b6;

   a1.ap1 = a2.ap1 = a3.ap1 = 0;
   a1.ap2 = a2.ap2 = a3.ap2 = 0;
   a1.ap3 = a2.ap3 = a3.ap3 = 0;

   b1.ap1 = b2.ap1 = b3.ap1 = b4.ap1 = b5.ap1 = b6.ap1 = 0;
   b1.ap2 = b2.ap2 = b3.ap2 = b4.ap2 = b5.ap2 = b6.ap2 = 0;
   b1.ap3 = b2.ap3 = b3.ap3 = b4.ap3 = b5.ap3 = b6.ap3 = 0;
   b1.bp1 = b2.bp1 = b3.bp1 = b4.bp1 = b5.bp1 = b6.bp1 = 0;
   b1.bp2 = b2.bp2 = b3.bp2 = b4.bp2 = b5.bp2 = b6.bp2 = 0;
   b1.bp3 = b2.bp3 = b3.bp3 = b4.bp3 = b5.bp3 = b6.bp3 = 0;

   a.af( );
   b.af( );
   b.bf( );

   return 0;
}

// These three calls will initialize any 'int' they can access, to a specific
// value. What value is not the issue. The real question is :
//
// For the three cases, which 'int's can be accessed, and which one can't ?
//
// For all three cases, and for each 'int' that exists, it must be made clear
// whether it can be accessed or not. Keep count with public, protected,
// private, and with all non-zero pointers.

//---------------------------------------------------------------------------

void A::af( void )
{
   ad1 = 7;
   ad2 = 7;
   ad3 = 7;
   bd1 = 7;           // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bd2 = 7;           // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bd3 = 7;           // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   ap1 -> ad1 = 7;
   ap1 -> ad2 = 7;
   ap1 -> ad3 = 7;
   ap1 -> bd1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap1 -> bd2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap1 -> bd3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   ap2 -> ad1 = 7;
   ap2 -> ad2 = 7;
   ap2 -> ad3 = 7;
   ap2 -> bd1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap2 -> bd2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap2 -> bd3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   ap3 -> ad1 = 7;
   ap3 -> ad2 = 7;    // myself |             |             |          | ***
   ap3 -> ad3 = 7;    // myself | Borland 3.1 |             |          | ***
   ap3 -> bd1 = 7;
   ap3 -> bd2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap3 -> bd3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   bp1 -> ad1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> ad2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> ad3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> bd1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> bd2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> bd3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   bp2 -> ad1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> ad2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> ad3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> bd1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> bd2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> bd3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   bp3 -> ad1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp3 -> ad2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp3 -> ad3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp3 -> bd1 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp3 -> bd2 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp3 -> bd3 = 7;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
}

//----------------------------------------------------------------------------

void B::bf( void )
{
   ad1 = 8;
   ad2 = 8;
   ad3 = 8;           // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bd1 = 8;
   bd2 = 8;
   bd3 = 8;

   ap1 -> ad1 = 8;
   ap1 -> ad2 = 8;    // myself | Borland 3.1 |             | g++2.7.2 |
   ap1 -> ad3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap1 -> bd1 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap1 -> bd2 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap1 -> bd3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   ap2 -> ad1 = 8;
   ap2 -> ad2 = 8;    // myself | Borland 3.1 |             | g++2.7.2 |
   ap2 -> ad3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap2 -> bd1 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap2 -> bd2 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap2 -> bd3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   ap3 -> ad1 = 8;
   ap3 -> ad2 = 8;
   ap3 -> ad3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   ap3 -> bd1 = 8;
   ap3 -> bd2 = 8;
   ap3 -> bd3 = 8;

   bp1 -> ad1 = 8;
   bp1 -> ad2 = 8;    // myself | Borland 3.1 |             | g++2.7.2 |
   bp1 -> ad3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> bd1 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> bd2 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp1 -> bd3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   bp2 -> ad1 = 8;
   bp2 -> ad2 = 8;    // myself | Borland 3.1 |             | g++2.7.2 |
   bp2 -> ad3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> bd1 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> bd2 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp2 -> bd3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |

   bp3 -> ad1 = 8;
   bp3 -> ad2 = 8;
   bp3 -> ad3 = 8;    // myself | Borland 3.1 | Zortech 2.1 | g++2.7.2 |
   bp3 -> bd1 = 8;
   bp3 -> bd2 = 8;
   bp3 -> bd3 = 8;
}

//---------------------------------------------------------------------------
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]