[comp.lang.c++] private to a class or to an object?

john@wizard.UUCP (John Danner) (09/15/89)

Why is the following piece of code acceptable to cfront 1.2?
class flug
{
  private:
  void printit()
    {
      printf("xy are %d %d\n",x,y);
    }  
  int x,y;
 public:
  flug* z;
  flug()
    {
    };
  void setxy(int a, int b)
    {
      x = a;
      y = b;
    }
  void printz()
    {
      z->printit();//no syntax error
      z->x = 6;//no syntax error
      z->y = 7;//no syntax error
      z->printit();
    }
  
};
  main()
{
  flug a,b;
  a.z = &b;
  b.setxy(1,0);
  a.printz();
//  printf("a.z->x = %s, a.z->y = %s\n",a.z->x,a.z->y);//syntax error private
}

Shouldn't private data be private to a single object and not accessible
to any object of that class?  Is this something that is safe to take
advantage of as far as compatibility with future versions of cfront?

John Danner       Tandem Computers, Inc.
Net: pacbell!tandem!wizard!john@decwrl.dec.com
UUCP:...!decwrl!pacbell!tandem!wizard!john

sakkinen@tukki.jyu.fi (Markku Sakkinen) (09/20/89)

In article <517@wizard.UUCP> john@wizard.UUCP (John Danner) writes:
> [example deleted]
>
>Shouldn't private data be private to a single object and not accessible
>to any object of that class?  Is this something that is safe to take
>advantage of as far as compatibility with future versions of cfront?

There are object-oriented languages in which the unit of encapsulation
is an object (e.g. Smalltalk), and those in which it is a class.
Both approaches have their pros and cons.
C++ belongs to the latter camp, and this is part of the fundamental
philosophy of C++, so you can safely rely upon it.

However:
The new restriction on access to _protected_ parts of objects from
subclass objects in C++ release 2.0 has been recently criticised
in this newsgroup, because some of us think it violates the clear
principle of class-level encapsulation.

Markku Sakkinen
Department of Computer Science
University of Jyvaskyla (a's with umlauts)
Seminaarinkatu 15
SF-40100 Jyvaskyla (umlauts again)
Finland

randolph@ektools.UUCP (Gary L. Randolph) (09/21/89)

In article <517@wizard.UUCP> john@wizard.UUCP (John Danner) writes:
=Why is the following piece of code acceptable to cfront 1.2?
=class flug
={
=  private:
=  void printit()
=    {
=      printf("xy are %d %d\n",x,y);
=    }  
=  int x,y;
= public:
=  flug* z;
=  flug()
=    {
=    };
=  void setxy(int a, int b)
=    {
=      x = a;
=      y = b;
=    }
=  void printz()
=    {
=      z->printit();//no syntax error
=      z->x = 6;//no syntax error
=      z->y = 7;//no syntax error
=      z->printit();
=    }
=  
=};
=  main()
={
=  flug a,b;
=  a.z = &b;
=  b.setxy(1,0);
=  a.printz();
=//  printf("a.z->x = %s, a.z->y = %s\n",a.z->x,a.z->y);//syntax error private
=}
=
=Shouldn't private data be private to a single object and not accessible
=to any object of that class?  Is this something that is safe to take
=advantage of as far as compatibility with future versions of cfront?

No, C++ is type based.  This was brought up several months ago but is 
well worth mentioning periodically.  The example in Stroustrup that points
this out is on page 175.  Tiny is a class that has operator=() overloaded
so that it accesses the private variable, v, of another Tiny object.