Python Productivity over C++ (Encapsulation)

Jeff Massung jmassung at magpiesystems.com
Thu Jun 15 17:36:45 EDT 2000


Hung Jung Lu wrote in message <20000615205922.99304.qmail at hotmail.com>...
>>I can easily subvert encapsulation in many C++ classes simply with the
line
>>"#define private public" before I include any header files. Perhaps this
>>means
>>that C++ will in fact lead to the overthrow of evil and oppression in the
>>world?

Many times this won't work:

class Dummy {
  int x;
public:
  /* ... */
};

x is atuomatically private in this case - without the private keyword.

>private data members. In my world there are no variables that can't be made
>public, ha! The thing is, if you have to constantly recurr to subversion to
>add functionalities, there is a problem with the language.

No - there is a problem with the (original) programmer - not the language.
And while yes, all variables CAN be public, IMHO, this is bad programming,
just use struct { ... } instead of class { ... } if that is what you want.

Having private/protected members is VERY good for not letting data
"accidently" or "intentially" changing on the object without it knowing.
Example? A string class:

class String {
  char *data;
  int len;
public:
  /* ... */
};

What if the programmer has access to data? He can alter it, and then the
class will produce incorrect results when using len.

Encapsulation is GOOD - our friend.

Oh well, just my .02 worth ;)
Jeff






More information about the Python-list mailing list