0

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"
    }
  };

3 Answers3

0

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).

Johan
  • 3,728
  • 16
  • 25
0

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.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Hmm... I think you are right. I fiddled around a Little bit and changed the method of mySubClass2. I casted the library obj to mySubClass Obj. myDerivedClass::mySubClass1* noMyObj = (myDerivedClass::mySubClass1*) obj – user3143855 Dec 29 '13 at 15:59
0

Because fResponse in mySubClass2 is treated as protected and at that point it is outside of libraryClass, it only worked on librarySubClass2 because it is inside libraryClass.

redwud
  • 174
  • 3
  • 8