Cannot overload virtual method?

Discussion in 'C++' started by Allen, Aug 27, 2008.

  1. Allen

    Allen Guest

    class SerializedObject
    {
    public:
    int setBytes(void * buffer, int length)
    { return setBytes(buffer, length, ByteOrder::LocalOrder()); }

    virtual int setBytes(void *buffer, int length, int order);
    ...
    };

    class MySerializedObject : public SerilizedObject
    {
    public:
    MySerializedObject() {}
    virtual ~MySerializedObject() {}

    public:
    int setBytes(void * buffer, int length, int order) { ... }
    ...
    };

    int main()
    {
    MySerializedObject obj;
    char buffer[8];
    obj.setBytes(buffer, 8); /* MARKED */
    return 0;
    }

    vs2005 tells that MySerializedObject class has no setBytes method
    defined which accepts two arguments at the MARKED line. Why and how to
    correct it?

    Thank you in advance.
    Allen, Aug 27, 2008
    #1
    1. Advertising

  2. Allen

    Leandro Melo Guest

    On Aug 26, 6:36 pm, Allen <> wrote:
    > class SerializedObject
    > {
    > public:
    >   int setBytes(void * buffer, int length)
    >   { return setBytes(buffer, length, ByteOrder::LocalOrder()); }
    >
    >   virtual int setBytes(void *buffer, int length, int order);
    >   ...
    >
    > };
    >
    > class MySerializedObject : public SerilizedObject
    > {
    > public:
    >   MySerializedObject() {}
    >   virtual ~MySerializedObject() {}
    >
    > public:
    >   int setBytes(void * buffer, int length, int order) { ... }
    >   ...
    >
    > };
    >
    > int main()
    > {
    >   MySerializedObject obj;
    >   char buffer[8];
    >   obj.setBytes(buffer, 8); /* MARKED */
    >   return 0;
    >
    > }
    >
    > vs2005 tells that MySerializedObject class has no setBytes method
    > defined which accepts two arguments at the MARKED line. Why and how to
    > correct it?



    Hi.

    Your sub-class is hiding the member function setBytes from the base
    class. This is due to C++ name lookup rules. A simple way to fix is
    with a using declaration:

    class MySerializedObject : public SerializedObject //There was a typo
    here.
    {
    public:
    MySerializedObject() {}
    virtual ~MySerializedObject() {}

    public:
    using SerializedObject::setBytes; //Notice here...

    int setBytes(void * buffer, int length, int order) { ... }

    };

    --
    Leandro T. C. Melo
    Leandro Melo, Aug 27, 2008
    #2
    1. Advertising

  3. Allen

    Rolf Magnus Guest

    Sam wrote:

    > Allen writes:
    >
    >> class SerializedObject
    >> {
    >> public:
    >> int setBytes(void * buffer, int length)
    >> { return setBytes(buffer, length, ByteOrder::LocalOrder()); }
    >>
    >> virtual int setBytes(void *buffer, int length, int order);
    >> ...
    >> };
    >>
    >> class MySerializedObject : public SerilizedObject
    >> {
    >> public:
    >> MySerializedObject() {}
    >> virtual ~MySerializedObject() {}
    >>
    >> public:
    >> int setBytes(void * buffer, int length, int order) { ... }
    >> ...
    >> };
    >>
    >> int main()
    >> {
    >> MySerializedObject obj;
    >> char buffer[8];
    >> obj.setBytes(buffer, 8); /* MARKED */
    >> return 0;
    >> }
    >>
    >> vs2005 tells that MySerializedObject class has no setBytes method
    >> defined which accepts two arguments at the MARKED line. Why and how to
    >> correct it?

    >
    > Why:
    >
    > Because MySerializedObject does not have
    > setBytes(void * buffer, int length), but a setBytes function with a
    > different prototype.
    >
    > It's a scope resolution quirk. If the compiler finds one or more functions
    > defined by the class with the requested name, it'll try to match up the
    > function signatures. If it can't, it'll report an error, it won't continue
    > searching any superclasses for the function of the matching signature.
    > It'll search the superclasses only if it does not find any function of the
    > given name in the subclass.
    >
    > How to fix:
    >
    > A)
    >
    > obj.SerializedObject::setBytes(buffer, 8);
    >
    > B)
    >
    > static_cast<SerializedObject &>(obj).setBytes(buffer, 0);


    C) Add to the derived class:

    using SerializedObject::setBytes;
    Rolf Magnus, Aug 27, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  2. jlopes
    Replies:
    7
    Views:
    407
    jlopes
    Nov 19, 2004
  3. asdf
    Replies:
    7
    Views:
    714
    David Harmon
    Oct 11, 2006
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Sherm Pendley
    Oct 11, 2004
  5. Lloyd
    Replies:
    10
    Views:
    262
    Victor Bazarov
    Aug 2, 2013
Loading...

Share This Page