简体   繁体   中英

access protected variable - complicated situation with inheritance and sub-classes

Hmm... I'm trying to break down my problem... There is a library with some classes that do almost what I want. I can't change classes of the library so I want to derive them and change what I need. In this case there is a derived class in the library with two subclasses. Now I derive the class and the subclasses. In the second sub-class there is a virtual method witch modifies a protected variable from the first sub-class. I want to override the virtual method with a new virtual method which calls the old virtual wethod an then modify the protected variable again.

Why am I getting the error in mySubClass2 while accessing fResponse?

How can I solve my problem?

class libraryClass : pulic someLibraryBaseClass {
protected:
  libraryClass::librarySubClass2 lookUpFunction(int ID) {
    //some magic to find the obj
    return obj;
  }

public:
  class librarySubClass2;
  class librarySubClass1 {
  public:

    librarySubClass1(libraryClass baseObj) {
      myBaseObj = baseObj;
    }

    void someCallingFunction(int ID) {
      libraryClass::librarySubClass2 obj = myBaseObj->lookUpFunction(ID)
      obj->someHandleFunction(this)
      cout << fResponse;
    }
  protected:
    friend class librarySubClass2;
    unsigned char fResponse[200];
  private:
    libraryClass myBaseObj;
  };
  class librarySubClass2 {
  protected:
    virtual void someHandleFunction(libraryClass::librarySubClass1* obj) {
      snprintf((char*)obj->fResponse, sizeof obj->fResponse, "Some Text...\r\n"
    }
  };
};


class myDerivedClass : public libraryClass {
public:
  class mySubClass2 : public libraryClass::librarySubClass2;
  class mySubClass1 : public libraryClass::librarySubClass1 {
  protected:
    friend class mySubClass2;
  };

  class mySubClass2 : public libraryClass::librarySubClass2 {
  protected:
    virtual void someHandleFunction(libraryClass::librarySubClass1* obj) {
      libraryClass:librarySubClass2::someHandleFuntion(obj);
      snprintf((char*)obj->fResponse, sizeof obj->fResponse, "Add some more Text...\r\n"
    }
  };
};

Edit: Forgot * in Method of mySubClass2

Possible solution:

  class mySubClass2 : public libraryClass::librarySubClass2 {
  protected:
    virtual void someHandleFunction(libraryClass::librarySubClass1* obj) {
      libraryClass:librarySubClass2::someHandleFuntion(obj);
      myDerivedClass::mySubClass1* nowMyObj = (myDerivedClass::mySubClass*) obj;
      snprintf((char*)nowMyObj->fResponse, sizeof nowMyObj->fResponse, "Add some more Text...\r\n"
    }
  };

Now I derive the class and the subclasses.

In your example code, you're only deriving the main class and not the subclass. You have to inherit also the subclass:

class libraryClass : pulic someLibraryBaseClass 
{
  class librarySubClass1 : public someLibraryBaseClass::someLibrarySubClass1 { };

  // ....
};

But that can be done only if the subclass is accessible (protected/public).

As far as I can tell you wonder why you can't access obj->fResponse in

void mySubClass2::someHandleFunction(libraryClass::librarySubClass1 obj) { ... }

Well, obj is of type librarySubClass1 which inherits its share of fResponse from the common ancestor. However, that is the share of a relative of mySubClass2 , not yours as you are mySubClass2 ! You can only access the fResponse member of objects which are known to be of type mySubClass which actually happens to be known to be not the case for a librarySubClass1 object.

Getting access to librarySubClass::fResponse is as if you got free access to your uncle's inheritance from your grandparents. Unless you have a very unusual family sharing its wealth freely among all family members, you probably won't have access to your uncle's inheritance either.

因为mySubClass2中的fResponse被视为受保护的,并且那时它在libraryClass之外,所以它仅在librarySubClass2上起作用,因为它在libraryClass内部。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM