Now I have code below:
class Env
{
public:
int ra(){ return a;}
int rb(){ return b;}
private:
int a;
int b;
};
class CEnv: private Env
{
public:
static Env* Instance()
{
CEnv* pEnv = new CEnv;
return pEnv;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Env* pEnv = CEnv::Instance();
pEnv->ra();
return 0;
}
And it works well.Later I add some code.
class Env
{
public:
int ra(){ return a;}
int rb(){ return b;}
private:
int a;
int b;
};
class CEnv: private Env
{
public:
static Env* Instance()
{
CEnv* pEnv = new CEnv;
return pEnv;
}
};
Env* test()
{
CEnv *p = new CEnv;
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
Env* pEnv = CEnv::Instance();
pEnv->ra();
return 0;
}
Then VS2010 will tell out compiler error : error C2243: 'type cast' : conversion from 'CEnv *' to 'Env &' exists, but is inaccessible.
In my opinion, it's right to show the error ,because it's not as-a relationship if using private herit. But the first code pattern works well. And I wonder why ?