Originally posted by black
did you mean all the private members are visible among those instances which are derived from the same class ?
No....for that you would need to make the data in the base class protected.
But 2 objects of the same class can access each other's private data. This is needed for situation like in the OP's original example (operator+()).....it's not a weakness as you might at this moment be thinking because you still cant access these variables from "the outside"
Code:
#include <iostream>
#include <string>
class foobar{
private:
std::string m_str;//private!
public:
foobar(const std::string& str):m_str(str){}
//constructor sets string
void PrintMyString(){std::cout << m_str;}
void PrintOtherString(const foobar& foo){
std::cout << foo.m_str;
}//has access to other object's private members
};
int main(void){
foobar f("Hello "),g("World");
f.PrintMyString();//Access to its own private data
f.PrintOtherString(g);//Access to other foobar's private data
//std::cout << f.m_str;
//Still cant access private members from outside
}