Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for a Visual studio 2005 c++ inheritance bug

The following code does not compile in Visual Studio 2005:

class OriginalClass
{
public:
    class Delegate
    {
        virtual void original_func()=0;
    };
};

class BaseClass
    :public OriginalClass::Delegate //  Problem line 1
{
public:
    class Delegate
    {
        virtual void base_func(int x) = 0;
    };

    void original_func()override{}  //  Problem line 2
};

class DerivedClass : public BaseClass::Delegate
{
public:
    virtual void base_func(int x) override {};
};

int main ()
{
    DerivedClass derived_object;

    derived_object.base_func(10);
}

The build output is:

1>inherit\main.cpp(26) : error C3668: 'DerivedClass::base_func' : method with override specifier 'override' did not override any base class methods
1>inherit\main.cpp(32) : error C2259: 'DerivedClass' : cannot instantiate abstract class
1>        due to following members:
1>        'void OriginalClass::Delegate::original_func(void)' : is abstract
1>        inherit\main.cpp(7) : see declaration of 'OriginalClass::Delegate::original_func'

If I comment out the lines marked Problem line 1 and Problem line 2 it builds OK. So, the use of override is not a problem, nor is inheriting from the nested class. It seems to have difficulty in figuring out which Delegate class is the correct one to use.

This problem does not exist in VC2008.

Can anyone suggest a workaround for this? Besides the obvious: upgrade to a modern compiler!

Also, I would appreciate it if anyone can point to any documentation of the bug (if it is a bug).

Edit: @Anonymous Coward suggested using typedefs, it will compile if the OriginalClass is changed to the following:

class OriginalClass
{
public:
    class Delegate_t
    {
        virtual void original_func()=0;
    };
    typedef Delegate_t Delegate;
};
like image 263
Eoghan Avatar asked Nov 03 '22 11:11

Eoghan


1 Answers

That seems to be a name resolution problem indeed. It compiles when using typedefs:

class OriginalClass {
    // class Delegate { ... };
    typedef Delegate delegate_t;
};

class BaseClass : public OriginalClass::delegate_t {
    // class Delegate { ... };
    typedef Delegate delegate_t;
};

class DerivedClass : public BaseClass::delegate_t {
    // ...
};
like image 192
Anonymous Coward Avatar answered Nov 12 '22 20:11

Anonymous Coward