Cannot overload virtual method?

A

Allen

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

Leandro Melo

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

};
 
R

Rolf Magnus

Sam said:
Allen said:
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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top