c++ - 函数指针?

标签 c++ pointers

说我有

class B{   //base class

}

class A : public B{ //derived class
}

我还有一个返回指向类 B 的指针的函数

B* returnB(){
    B * object = new A; //pointer of base class allocate object of derived class
    return object;
}

现在当我尝试创建一个指向函数 B* 的指针时,我得到一个错误

B* (*randomFunction)();
randomFunction = returnB;  

Visual Studios 无法编译。

1   IntelliSense: a value of type "B*(MediaFactory::*)()" cannot be assigned to an entity of type "B*(*)()" c:\Users\...\mediafactory.cpp   35

最佳答案

您似乎试图将指向类 MediaFactory 的成员函数的指针分配给可以容纳非成员函数的变量。这些实体不兼容。使用 boost bind 或将函数指针变量更改为 B* (MediaFactory::*)() 类型。

关于c++ - 函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413789/

相关文章:

c++ - 如何在 Visual Studio 2010 中使用互斥锁

C++可以setw和setfill填充字符串的末尾吗?

c++ - C++ 的非线程异步 IO 简介?

c++ - 从结构中引用对象的指针

c++ - 指针映射的数组下标运算符

c - 在 c 中访问/使用 void 指针时出错

c++ - malloc:无效指针从空闲列表中出队

c++ - 函数模板作为成员 - GCC 与 CLANG

c++ - 无法将参数 'std::basic_string<char>' 的 'const char*' 转换为 '1' 到 'int system(const char*)'

c - 关于C中函数参数的静态值