samedi 15 juillet 2017

friend of a derived class can't acess derived class data members,instead can access only base class members

WRT below code, the friend class member function isnot able to access the members of the derived class eventhough it's a friend of it. However, this friend class is able to access members of base class eveb=nrhough it's not a member of it. Starnge. Why is it ?

class Base
{
public:
int a;
float b;
...
static Base* CreateObj(ChildType ch)
{
    if(ch == child1Type())
        return new ChildType();
}
..
}

class ChildType:public Base
{
   public:
  int c;
  float d;
  friend class frie;
  frie FrieObj;
}

class frie
{
   public:
    AccessFunc()
    {
        a=2; //can access base class 
        c =3; // Can't access derived class
    }

Aucun commentaire:

Enregistrer un commentaire