Inheriting template-based assignment operator [duplicate]

  c++, inheritance, oop, visual-c++

Given the following code:

template <typename T, T DefaultValue>
class CObjectManager
{
private:
    T m_Object;

public:
    CObjectManager(const T object = DefaultValue)
    {
        m_Object = DefaultValue;
    }

    ~CObjectManager()
    {
        DeleteObject();
    }

    T GetValue()
    {
        return m_Object;
    }

    void operator =(const T object)
    {
        DeleteObject();
        m_Object = object;
    }

protected:
    void DeleteObject()
    {
        if (m_Object != DefaultValue)
        {
            FreeObject(m_Object);
            m_Object = DefaultValue;
        }
    }

    virtual void FreeObject(T object) = 0;
};

class CBCryptAlgorithmProviderManager : public CObjectManager<BCRYPT_ALG_HANDLE, NULL>
{
    void FreeObject(BCRYPT_ALG_HANDLE object)
    {
        ::BCryptCloseAlgorithmProvider(object, 0);
    }
};

I expected the following to work.

BCRYPT_ALG_HANDLE hAlgorithm;

status = BCryptOpenAlgorithmProvider(&hAlgorithm, BCRYPT_AES_ALGORITHM, NULL, 0);
if (status != STATUS_SUCCESS)
    throw new CEncryptionException(status, _T("Unable to open encryption provider."));

provider = hAlgorithm;

But the last line gives an error:

no operator "=" matches these operands : operand types are CBCryptAlgorithmProviderManager = BCRYPT_ALG_HANDLE

It’s probably something stupid on my part. But shouldn’t this work?

Source: Windows Questions C++

LEAVE A COMMENT