- #1
yungman
- 5,718
- 241
I read about forward declaration on line, it's pretty straight forward. But I don't understand this program from the book:
Line#3 in Auxil.h is Forward Declaration. Line#11 in Budget3.h declare friend void AuxOffice::addBudget(double, Budget3&);
This is Auxil.h
This is Budget3.h
1) What I don't quite get is there is no declaration to #include Budget3.h in Auxil.h at the beginning. How does the compiler knows to go to Budget3.h? Is it because Budget3.h declare FRIEND in line 11 that give the permission to the compiler to go out of Auxil.h to search for class Budget3 in budget3.h?
2) Is it true that when Budget3.h declares friend void AuxOffice::addBudget(double, Budget3&); in line 11, the member function b has to pass class Budget3 as reference so there is only one copy of Budget3 in the program?
I am still not comfortable with the FRIEND thing.
Thanks
Line#3 in Auxil.h is Forward Declaration. Line#11 in Budget3.h declare friend void AuxOffice::addBudget(double, Budget3&);
This is Auxil.h
C++:
#ifndef Auxil_H
#define Auxil_H
class Budget3;//Forward declaration of Budget class
class AuxOffice
{ private: double auxBudget;
public:
AuxOffice() { auxBudget = 0; }//Constructor set auxBudget = 0.
double getDivBudget() const { return auxBudget; }//Something wrong, no return
void addBudget(double, Budget3&);
//{auxBudget += fromA; div.corpBudget += fromA;}
};
#endif // !Auxil_H
This is Budget3.h
C++:
#ifndef Budget3_H
#define Budget3_H
#include <iostream>
#include "Auxil.h"
class Budget3
{ private: //corpBudget is static, cannot be declared in Budget3.h, use Budget3.cpp
static double corpBudget; double divBudget;
public: Budget3() { divBudget = 0; }//Constructor starting divBudget = 0
.
.
friend void AuxOffice::addBudget(double, Budget3&);
};
#endif
1) What I don't quite get is there is no declaration to #include Budget3.h in Auxil.h at the beginning. How does the compiler knows to go to Budget3.h? Is it because Budget3.h declare FRIEND in line 11 that give the permission to the compiler to go out of Auxil.h to search for class Budget3 in budget3.h?
2) Is it true that when Budget3.h declares friend void AuxOffice::addBudget(double, Budget3&); in line 11, the member function b has to pass class Budget3 as reference so there is only one copy of Budget3 in the program?
I am still not comfortable with the FRIEND thing.
Thanks