B
Bryan Parkoff
CMain Class is the base class that is initialized in main function. CA
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always private. I have placed "friend
void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don't understand why because friend should be able to access private
function. Please advise.
/* A.h */
class CMain;
class CA
{
public:
CA(CMain* pMain);
~CA();
private:
CA();
void Run_A(void);
CMain* m_pMain;
};
/* A.cpp */
#include "A.h"
CA::CA()
{
}
CA::CA(CMain* pMain)
{
m_pMain = pMain;
}
CA::~CA()
{
}
void CA::Run_A(void)
{
}
#include <stdio.h>
#include "A.h"
class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void);
private:
CA* m_pA;
};
CMain::CMain()
{
m_pA = new CA(this);
}
CMain::~CMain()
{
delete m_pA;
}
void CMain::Run(void)
{
CA::Run_A();
}
void main(void)
{
CMain Main;
}
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always private. I have placed "friend
void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don't understand why because friend should be able to access private
function. Please advise.
/* A.h */
class CMain;
class CA
{
public:
CA(CMain* pMain);
~CA();
private:
CA();
void Run_A(void);
CMain* m_pMain;
};
/* A.cpp */
#include "A.h"
CA::CA()
{
}
CA::CA(CMain* pMain)
{
m_pMain = pMain;
}
CA::~CA()
{
}
void CA::Run_A(void)
{
}
#include <stdio.h>
#include "A.h"
class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void);
private:
CA* m_pA;
};
CMain::CMain()
{
m_pA = new CA(this);
}
CMain::~CMain()
{
delete m_pA;
}
void CMain::Run(void)
{
CA::Run_A();
}
void main(void)
{
CMain Main;
}