classes

I don't know how can a function of a class use private member declared in another class?!
With 99.9% probability, if you need to do that, then you have bad program desing.
In case you think it is one of these 0.1%: use friend functions: http://www.learncpp.com/cpp-tutorial/813-friend-functions-and-classes/
An example of where it is very useful is when overloading operators for which your class is on the right. Example:
1
2
3
4
5
6
7
8
9
10
class Foo
{
    int bar;
    friend std::ostream& operator<<(std::ostream&, Foo&);
};

std::ostream& operator<<(std::ostream& o, Foo& f)
{
    return o<<f.bar;
}
Last edited on
Topic archived. No new replies allowed.