9

I am having a bit of a struggle with Microsoft Visual C++ 2015 and was able to replicate the issue with a small program. Given the following classes:

class BaseClass {
public:
    BaseClass()
        : mValue( 0 )
        , mDirty( true )
    {}
    virtual ~BaseClass() {}
    virtual int getValue() const { if( mDirty ) updateValue(); return mValue; }

protected:
    virtual void updateValue() const = 0;

    mutable bool mDirty;
    mutable int  mValue;
};

class DerivedClass : public BaseClass {
public:
    DerivedClass() {}

protected:
    void updateValue() const override
    {
        mValue++;
        mDirty = false;
    }
};

class Impersonator {
public:
    Impersonator() {}

    // conversion operator
    operator DerivedClass() const
    {
        return DerivedClass();
    }

    // conversion method
    DerivedClass toDerived() const
    {
        return DerivedClass();
    }
};

I get a "pure virtual function call" error when I do the following:

void use( const BaseClass &inst )
{
    // calls `getValue` which in turns calls the virtual function 'updateValue'
    int value = inst.getValue();
}

int main()
{
    // creates a temporary, then passes it by reference:
    use( DerivedClass() ); // this works

    // calls conversion operator to create object on stack, then passes it by reference:
    DerivedClass i = Impersonator();
    use( i ); // this works

    // calls conversion method to create a temporary, then passes it by reference:
    use( Impersonator().toDerived() ); // this works

    // calls conversion operator to create a temporary, then passes it by reference:
    Impersonator j = Impersonator();
    use( j ); // causes a pure virtual function call error!

    return 0;
}

Given that I can't change the void use(const BaseClass&) function, can I change anything in the Impersonator class to allow using the last call without generating a debug error?

19
  • 1
    If you breakpoint inside the last call to getValue and inspect the vtable pointer, MSVC thinks you have a BaseClass object, which looks incorrect.
    – Praetorian
    Aug 1, 2016 at 17:26
  • 2
    Cannot reproduce. MVC considered bad.
    – Shoe
    Aug 1, 2016 at 17:29
  • 3
    Inspecting the resulting assembly shows that for some reason or other MSVC decides to call BaseClass::BaseClass to copy the temporary returned from operator DerivedClass despite BaseClass being abstract. Explicit declaration of the copy constructor as non-public makes MSVC complain: error C2248: 'BaseClass::BaseClass' : cannot access protected member declared in class 'BaseClass'.
    – ach
    Aug 1, 2016 at 17:58
  • 2
    @AndreyChernyakhovskiy I don't know. I have constructed a small test case. Old gcc and msvc print "gotcha", new gcc and clang do not. Why do old gcc and msvc want to copy-construct Base out of Derived? Aug 1, 2016 at 19:43
  • 3
    @AndreyChernyakhovskiy MSVC is most definitely in error since BaseClass is abstract and objects of this class should never be created. If copy construction is somehow the correct action (which seems totally incredible to me) MSVC should complain that BaseClass is abstract, not silently proceed to copy-construct it. Aug 1, 2016 at 20:12

2 Answers 2

2

The only way to mitigate the problem that I see is to add an operator const BaseClass&() to Impersonator and have it return a reference to DerivedClass.

This will create a better conversion than the problematic/erroneous one the compiler is trying to use.

Naturally Impersonator won't be able to return by value and create a temporary, so it will have to own a DerivedClass object, or many objects, and dispose them somehow at an appropriate time. The simplest way that works for this demo program is to have it return a reference to its data member, but a real program may have to do something else.

class Impersonator {
public:
    Impersonator() {}

    // conversion operator
    operator DerivedClass()
    {
        return d;
    }
    operator const BaseClass&()
    {
        return d;
    }

private:
    DerivedClass d;
};
3
  • Yes, that works. Unfortunately the "impersonator" in the actual application is a LightSource class trying to impersonate a Camera and its data members should strictly follow the std140 layout rules, so I can't add a DerivedClass data member. I could create a static one in the conversion operator, but then only one such "temporary" can be active and it's also not thread safe. Thanks for your input, though.
    – Paul Houx
    Aug 1, 2016 at 21:08
  • Partial solution: if I use a method on Impersonator to create a DerivedClass, instead of a conversion operator, it works: DerivedClass toDerived() const { return DerivedClass(); }. I'll add this to the sample code.
    – Paul Houx
    Aug 1, 2016 at 21:25
  • You can allocate DerivedClass objects dynamically, and place them in a thread-local container. Clean it up when you know no DerivedClass objects can possibly be needed. Or, if you can wrap use, delete the dynamic object in the wrapper. Aug 1, 2016 at 21:33
1

This is a workaround. Create a wrapper for use which accepts a const DerivedClass&.

//I get a "pure virtual function call" error when I do the following :
void use(const BaseClass &inst)
{
    // calls `getValue` which in turns calls the virtual function 'updateValue'
    int value = inst.getValue();
}

void use(const DerivedClass &inst) {
    use(static_cast<const BaseClass&>(inst));
}

The better match means the workaround wrapper will be selected, so a temporary of the correct type will be created, and a reference to that passed to the real use implementation.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.