3

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?

Allan Jiang
  • 11,063
  • 27
  • 104
  • 165
  • if the args are different then you will have 2 member functions called RuntimeClassInitialize. The one from B will not override the one from A – cppguy Aug 08 '14 at 22:22
  • @cppguy `ClassA` and `ClassB` takes different numbers of parguments. But I am passing the right args to the ClassB's `MakeAndInitialize` function. Magically it goes to class A as a result... – Allan Jiang Aug 08 '14 at 22:32
  • @AllanJiang Do you still see this problem? It's fine on my machine. – Eric Z Aug 12 '14 at 05:38

1 Answers1

3

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.

ArnonZ
  • 3,822
  • 4
  • 32
  • 42