hello
what is the relationship between those two classes;
do i have access in both's private data from both classes or not;
i saw that i cant access cs2 private data through cs1
also i read in a book that if i dont declare cs2 as a friend to cs1 i cant access cs1's private data through cs2.
but with some tests i had access without declare cs2 a friend to cs1.
do i have to declare cs2 as a friend to cs1 or i have access anyway because cs2 is a mebmer of cs1?
thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class cs1
{
private:
.....
public:
.....
class cs2
{
private:
.....
public:
.....
};
};
Do you define an object of class cs2 into cs1? I don't see any. Assuming you have (I guess you just don't put it in this piece of code) object of class cs2 does not know about the existence of class cs1. How could anyway? Definition of that class could be anywhere. It's an object of class cs2 into cs1. It just prevents this way the construction of object cs2 outside sc1 objects
So private members of cs2 can only be access through accessors of using friendship. If your object of class cs2 is private (as it should normally) you can access it only through accessors as all your private members of your cs1. For example to return a (private) data member of cs2 use an public accessor of cs2 called inside a public member of cs1. Something like: int sc1::get_i_of_cs2() const { return cs2_object_inside_cs1.get_i_of_cs2();}
The other way around I think it's more difficult. To give access to class cs2 of members of cs1 object of class cs2 has to know that it is created inside a cs1 object. I think it does not know that and so you cannot have access. Maybe I am wrong but I don't get how to do it.