Casting issue

Help
Bel Od
2004-07-07
2004-07-08
  • Bel Od

    Bel Od - 2004-07-07

    I have the following set of classes

    PartMessageIF --interface that extends--> Object
    PartMessageFile --implements--> PartMessageIF
    MultiPartMessage --makes use of --> PartMessageFile

    In the following method of class MultiPartMessage, I store a PartMessageFile in a
    Vector.  The only way I found to add PartMessageFile to the Vector was
    to downcast RPartMessageFile to an RObject.  That was unexpected to me.
    Is it because the interface PartMessageIF extends Object but it has no referenceable
    object RPartMessageIF?  Should I use an abstract class instead of an interface?

    /**
    *  This method adds a file as a message part
    */
    void MultiPartMessage::addPart(RString name, RString dirName, RString fileName) {
        RPartMessageFile partMessageFile = new PartMessageFile();
        partMessageFile->addPart(name, dirName, fileName, boundary);
        //partMessageVec->addElement((PartMessageIF)partMessageFile);  // <-- does not compile
        //partMessageVec->addElement(partMessageFile);  // <--does not compile
        partMessageVec->addElement((RObject)partMessageFile);
    }

     
    • Bel Od

      Bel Od - 2004-07-07

      To reword my concern here is a small piece of code
      that I cannot compile:

        RVector v = new Vector();
        RString str = new String("aaaa");
        v->addElement(str);

      I get the following error message:

      error C2664: 'addElement' : cannot convert parameter 1 from 'class acdk::lang::RString' to 'const class RefHolder<class acdk::lang::Object>

      I believe that this illustrates better my problem.
      Any idea?

       
    • Roger Rene Kommer

      > PartMessageIF --interface that extends--> Object
      ACDK has a similar object modell as Java. One Object must not be derived from Object multiple times. Multiple inherintence is only possible via interfaces. ACDK interfaces are not derived from Object. Please refer to acdk::lang::Comparable as example.

      PartMessageFile --implements--> PartMessageIF
      MultiPartMessage --makes use of --> PartMessageFile

      > RVector v = new Vector();
      > RString str = new String("aaaa");
      > v->addElement(str);
      Unfortunatelly the RefHolder<T> (which implements for example RObject, RInteger, etc.) rely in casting issues on some special template features.
      Some compiler will compile the code, other doesn't it.
      > v->addElement(str);
      Can be solved this way:
      > v->addElement((RObject)str); // hard cast
      but than you will not receive an compiler error/warning.
      > v->addElement(&str);
      Should warn if str is not a derived class from the expected parameter type.
      Please refer also to:
      http://acdk.sourceforge.net/acdk/docs/hb/lang/acdk_hb_lang_casting.html

      BTW: What compiler do you use?
      Roger

       
    • Bel Od

      Bel Od - 2004-07-08

      Thanks, I will try v->addElement(&str); and I will see how it behaves.  I will also fix my Interface deriving from Object.

      My compiler at work is the one used by Visual C++ 6.0 on Windows 2000.  All my developments are done at work.  At home I compiled also ACDK with success on Linux 2.4.22 and Mandrake 9.2, but currently I am not developing with ACDK at home.

       

Log in to post a comment.