无法使用带有MSVC的typedef声明纯虚函数

时间:2014-10-31 22:51:16

标签: c++ visual-c++

为什么我不能使用函数typedef来声明MSVC的纯虚函数?这适用于GCC。

在此代码段中,function存在链接错误,但function2Minimum working example in codepad with gcc),(Minimum working example in Coliru with clang)没有链接错误。

编辑:确切的链接错误是: error LNK2001: unresolved external symbol "public: virtual int __cdecl cv::of2::Base::function(int)" (?function@Base@of2@cv@@UEAAHH@Z)

#include <iostream>

class Base
{
public:

    typedef int func_t (int x);

    virtual func_t function = 0;

    virtual int function2(int x) = 0;
};

class Derived : public Base
{
public:
    func_t function;
    int function2 (int x);
};

int Derived::function(int x)
{
    return x;
}

int Derived::function2(int x)
{
    return x;
}

int main()
{
    Base * theClass = new Derived();

    std::cout << theClass->function(1) << std::endl;
    std::cout << theClass->function2(2) << std::endl;

    delete theClass;

    return 0;
}

如果我想强制多个函数具有相同的签名,这很有用,所以我可以将它们分配给函数指针。

0 个答案:

没有答案