Originally Posted by
deoren
So if I'm understanding you right, the CCarton class could be declared a friend of CBottle, and the defintion of CCarton wouldn't be required (forward declaration in place of the #include line?) yet?
No, the definition of CCarton is needed because CBottle.h references the constructor, not just the class. So it should look like:
Code:
// $Id$
// $HeadURL$
#pragma once
#include "CCarton.h"
class CBottle
{
public:
CBottle(double height, double diameter);
private:
double m_Height; // Bottle height
double m_Diameter; // Bottle diameter
// Let the carton constructor in
friend CCarton::CCarton(const CBottle& aBottle);
};
There is a sort of issue here in so far as using a relative path ("CCarton.h" instead of, eg. <CContainers/CCarton.h>) may complicate things depending on the context in which these are used -- but that is less of an issue than needing to specify the order of the includes correctly.
Also, it would be better (more standardized) to use include guards instead of pragma once:
Code:
#ifndef CBOTTLE
#define CBOTTLE
#include "CCarton.h"
class CBottle
{
public:
CBottle(double height, double diameter);
private:
double m_Height; // Bottle height
double m_Diameter; // Bottle diameter
// Let the carton constructor in
friend CCarton::CCarton(const CBottle& aBottle);
};
#endif