I was trying to understand friend functions and I found myself writing the following code
though I have understood the friend functions, this code leaves me with new questions:
- how does the class initialise here when I havn't instantiaied any object
I know
staticmember is shared by all objects of the class and is initialized to zero when the first object is created
- at what point does the variables
base_iandderived_iget assigned to respective values from code
I suppose it happens at
return derived::derived_i + derived::base_i;
- if so, does that also allocate the memory for all the other members of class at that point, specifically also for
newVarin this case
#include <iostream> class base { private: static int base_i; float newVar; public: friend int addClasses(); }; int base::base_i = 5; class derived : private base { private: static int derived_i; public: friend int addClasses(); }; int derived::derived_i = 3; int addClasses() { return derived::derived_i + derived::base_i; } int main() { std::cout<<addClasses()<<std::endl; }