c++windowsvisual-c++windows-runtimewrl

C++ Extend Windows WRL component class not working


I have a class ClassA implement interface IFooBar like below

class ClassA : public Microsoft::WRL::RuntimeClass<IFooBar>
{
public:
    virtual HRESULT RuntimeClassInitialize(ParamsForClassA);
}

Now I want to write a ClassB inherent ClassA and override it's RuntimeClassInitialize function like below:

class ClassB : public ClassA
{
public:
    HRESULT RuntimeClassInitialize(ParamsForClassB)
    {
        // implementation goes here
    }
}

And I create a pointer to ClassB object like this:

ComPtr<ClassB> ptr;
HRESULT hr = MakeAndInitialize<ClassB>(&ptr, ParamsForClassB);

But this actually goes to ClassA's RuntimeClassInitialize constructor. The ClassB's RuntimeClassInitialize code path is never hit.

I am wondering if this is the correct way of extending class in WRL? Where am I doing wrong in the code?


Solution

  • You need the overridden methods to have the same signature on both classes.

    this

    HRESULT RuntimeClassInitialize(ParamsForClassA);
    

    Can't be overridden by this

    HRESULT RuntimeClassInitialize(ParamsForClassB);
    

    Because they take different arguments.
    It's an overload, not an override. (And such overloads cause Name Hiding)

    You can find info on signatures here or here.