2

Can anybody explain what mechanism makes this invalid code?

I guess I would have expected the template member to be inherited as well, but it seems the compiler says no.

#include <string.h>

class MyBaseClass
{
public:
    virtual int read(unsigned uBytes, void*ptr)=0;
    template<typename T>
            int read(T &var) { return read(sizeof(T),&var);}
};

class MyDerivedClass : public MyBaseClass
{
public:
    virtual int read(unsigned uBytes, void*ptr)
    {
        memset(ptr,uBytes,uBytes);
        return uBytes;
    };
};

int main()
{
    unsigned a;
    MyDerivedClass thang;
    MyBaseClass &baseThang=thang;

    baseThang.read(a);  //ok
    thang.read(a);      //not ok
    return 0;
}
Russ Schultz
  • 2,545
  • 20
  • 22

0 Answers0