Re: [Dev-C++] C++ Question (the real one)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Bolek V. <vr...@pa...> - 2001-04-21 20:21:00
|
> Ioannis Vranos wrote:
>
> Ok i was stupid for my previous question. The answer is because it was
> a friend function. Here is the real question:
>
> #include <iostream>
>
> class Matrix
> {
> double m[4][4];
>
> public:
> void test(Matrix s);
> };
>
>
> void Matrix::test(Matrix s)
> {
> for (int i=0; i<4; i++)
> {
> std::cout<<std::endl;
>
> for(int j=0; j<4; j++)
> std:: cout<<s.m[i][j]<<std::endl;
> }
> }
>
> int main()
> {
>
> return 0;
> }
>
> The member function test() accesses the private data member of its
> argument directly. When is it allowed for functions to access directly
> the private members of their arguments except of those declared
> as friends?
It is allowed for friends and member functions of the same class (class
A) only - but it is allowed regeradless of whether it accesses data
members of its own instance (*this) or another instance of the class.
You can also declare a whole class B as a friend. This will cause all
member functions of class B to be friends of class A.
--
============================================================
Ing. Boleslav Vrany
Stanice techniku DDM hl. m. Prahy
Pod Juliskou 2a
Praha 6 - Dejvice
16000
Czech Republic
tel. (420 2) 243 10 240
|