// PhysicalTraits.h
struct PhysicalTraits {
unsigned int age; // years
double height; // meters
PhysicalTraits(const double H = 0.0, const unsigned int A = 0u) : age (A), height(H) {};
~PhysicalTraits() {};
};
// People.h
#include <PhysicalTraits.h>
class People {
PhysicalTraits traits;
public:
People(const double H = 0.0, const double A = 0u) : traits(H, A);
~People() {};
void displayAge() { std::cout << "You are " << traits.age << " years old." << std::endl; }
void displayHeight() { std::cout << "You are " << traits.height << " meters tall." << std::endl; }
};
// Me.h
#include <PhysicalTraits.h>
class Me {
PhysicalTraits traits;
public:
Me(const double H = 0.0) : traits(H);
~Me() {};
void displayAge() {
// I want `traits.age` to throw an error
std::cout << "You are " << traits.age << " years old." <<
}
void displayHeight() {
std::cout << "You are " << traits.height << " meters tall." << std::endl;
}
};
I intend to allow objects of type People
to access traits.age
. However, I don't want to know my age, so I want to restrict objects of type Me
from accessing traits.age
.
To do this, I modified PhysicalTraits.h
:
#include <People.h>
struct PhysicalTraits {
double height; // meters
private:
unsigned int age; // years
friend class People; // Objects of type `People` can access age
};
With age
as a private member variable and as a friend with People
, but not Me
. I've solved the problem... kind of.
However, the new problem is that I've included People.h
in PhysicalTraits.h
and PhysicalTraits.h
in People.h
. So, when PhysicalTraits.h
is compiled, before defining the PhysicalTraits
object it will jump to the definition of the People
object, which requires that the PhysicalTraits
object is defined.
How can I avoid this "mutual-include" problem while still ensuring that an object of type Me
cannot access traits.age
?