Visual C++ - Virtual method is not overriden

71 views Asked by At

I have two classes:

template <class T>
class TArray
{
public:
    TArray& operator =(const TArray &array) { return Copy(array); }
    virtual TArray& Copy(const TArray &array) { ... }
}

class TString : public TArray<TCHAR>
{
public:
    TString& Copy(const TString &string) { ... }
}

Classes also have needed constructors. But method Copy in second class does not override method in first class. Consider this code:

TString a = _T("aaa");
TString b;
b = a;

In third line program enters assignment operator in TArray. In it this and array are really of type TString. But when Copy is called, program enters method TArray::Copy, not TString::Copy as I expected. Why?

I'm using Visual Studio 2015 RC, but I'm moving some code from Visual Studio 6.0 project, and I'm pretty sure it has worked in it. Is something changed?

2

There are 2 answers

0
Luchian Grigore On BEST ANSWER

You're not overriding anything because the methods have different signatures:

TArray& Copy(const TArray &array)

vs

TString& Copy(const TString &string)
0
Rndp13 On

The signatures should be same for overriding. In your case,there are two different signatures.

TArray& Copy(const TArray &array)
TString& Copy(const TString &string)