Thursday, November 4, 2010

C ++ of protected

When I was in for c++ do major development languages in the last few years, I have not used protected. From the bottom pulls out once a favorite book: the c++ language design and evolution ", the Chinese version of 235 pages this record:

“ ... Mark Linton went to my Office for a moment, a make impressive requests, requests for the third control hierarchy to support Stanford University are developing a library of Interviews with style. We speculate that create the word protected to indicate that some members of the class, ... "

“... Mark is the main designer Interviews. His persuasive argument is based on practical experience and from real code examples. ...”

“... About five years after the Mark in the Interviews was to prohibit a member of the protected data, because they have become the source of many bugs ... "

I don't like protected, but today, I occasionally use C + +, is no longer so much fussier. Anyway, it is difficult to make using C + +, stable design, like how to use how to use it. The key is not in c++ do special core stuff.

Today, when a protected, a little depressed. Think you can write about. This is a fundamental problem that looks like the old familiar. After all, many years do not touch, for c++ syntax is a bit rusty.

When I was young, I once thought that this code is not legitimate.
----------------------------------------------
class foo {
  int a;
public:
  int foobar(foo * f) {
    return this->a + f->a;
  }
};
----------------------------------------------
class foo {
protected:
  int a;
};

class foobar : public foo {
public:
  int bar(foo * f) {
    return this->a + f->a;
  }
};
----------------------------------------------
class foo {
protected:
  int a;
  static int get_a(foo *self) {
    return self->a;
  }
};

class foobar : public foo {
public:
  int bar(foo * f) {
    return this->a + get_a(f);
  }
};
---------------------------------------------   
 

No comments:

Post a Comment