So, just how private is private, Private?

In C++, to what extent are the private parts of a class really truly private?
Quick — assuming that the Twice functions are defined in another translation unit that is included in the link, should the following C++ program compile and run correctly? If no, why not? If yes, what is the output?

// Example 1: Twice(x) returns 2*x
//
class Calc {
public:
  
double              Twice( double d );
private:
  
int                 Twice( int i );
  std::complex
<float> Twice( std::complex<float> c );
}
;

int main() {
  Calc c;
  
return c.Twice( 21 );
}

At the heart of the solution lies this question: In C++, to what extent are the private parts of a class, such as Example 1's Twice, really truly private? In this column, I'll show how private names are definitely not accessible from outside nonfriend code, and yet they can and do leak out of a class in small ways — some of which are well-known, others of which aren't, and a few of which can even be done as a calculated, deliberate act.

 

posted on 2004-07-23 17:43  LeighSword  阅读(340)  评论(1编辑  收藏  举报

导航