nested friend classes implementation

why does a friend class need a object to access the private functions of the "otherclass" , but the "otherclass" member functions dont need an object to access the private members

For example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Hen.h file

class Hen{

public:
	
	Hen();

	class Nest { 
		class Egg;
		friend class Egg;
			
		void access2();
		
	public:
	
		class Egg {
			class Nest;
			friend class Nest;

			void	access1();

		public:
			
			void access11(Hen::Nest*);
		};
	};	
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

//Hen.cpp file

#include "Hen.h"
#include <iostream>

void Hen::Nest::Egg::access1(){

	std::cout << "hen can access private egg";
	
}

void Hen::Nest::Egg::access11(Hen::Nest* n1){
	
	std::cout << " access public egg";
	
	Hen::Nest::Egg::access1(); // no object needed!

    // Hen::Nest::access2();         // is wrong
	n1->access2();                   // u need a object to call access 2 ,but not access1;
	                	                	
}

void Hen::Nest::access2(){
	std::cout << " accessing private nest";
}
Last edited on
Hen::Nest::Egg::access11(Hen::Nest*) can call Hen::Nest::Egg::access1() without specifying an object because they both are members of the same class. Note that that doesn't mean there is no object involved. You always need an object to call a member function. It's just that inside member functions the object that the function was called on is implicit and can be accessed by using the this keyword. What actually happens on line 17 is this: this->Hen::Nest::Egg::access1(); but you could write it as short as access1(); if you wanted.
Last edited on
This example is compiling fine ??!??? just because I made the function inline !??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

class Surround
    {
        static int s_variable;
        public:
            class FirstWithin
            {
                friend class Surround;
                static int s_variable;
                public:
                    int value();
            };
            int value();
        private:
            class SecondWithin
            {
                friend class Surround;
                static int s_variable;
                public:
                    int value();
            };
    };
    inline int Surround::FirstWithin::value()
    {
        FirstWithin::s_variable = SecondWithin::s_variable;
        return (s_variable);
    }
what is the explanation..?
are there anymore cases like the above or what..?
thanks!
C++11 ยง3.2/3
Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined. An inline function shall be defined in every translation unit in which it is odr-used.

So functions that are not inline should only be defined once in the program. That is why you can't define it in the header because that would mean it will be defined once for each source file (.cpp) that includes it.

Inline functions on the other hand has to be defined for each source file that calls the function. This more or less forces inline functions to be defined in headers.

Why this inconsistency you might ask. I think it has to do with the fact that it's much harder for the compiler to inline a function if the definition is not available. Remember that each source file is compiled in separation with no knowledge of other source files and not until everything has been compiled it is linked together to create an executable. Modern compilers usually can make it work by using something called link-time optimizations (e.g. GCC's -flto flag) but traditionally compilers have not had this feature and you still have to enable it because it makes the compilation process slower.
Sorry if I am not able to get your last post properly but ...
I know I can define inline functions in header files but does that mean, inside inline functions, u can force access of private members without an object? i mean other than "this"


and does implicit vs explicit have a performance difference in terms of run time and compile time

for eg:
is there a run time or compile time difference seen even by a small margin if i used any one of the following in my 1st
post in this topic:

1
2
3
4
access1();
this->access1();
Hen::Nest::Egg::access1();
this->Hen::Nest::Egg::access1();

Last edited on
Sorry, I might have misinterpreted your question. I thought you where shifting to a new question about inline.

No inline functions does not change how you can access things. The same rules apply.

Doing something implicit or explicit doesn't affect runtime performance at all.

To explain your original question

Surround is a friend of Surround::SecondWithin which means that the members of Surround are allowed to access the private members of Surround::SecondWithin.

Surround::FirstWithin is a member of Surround so it is perfectly fine for Surround::FirstWithin::value() to access Surround::SecondWithin::s_variable.
Last edited on
Topic archived. No new replies allowed.