简体   繁体   English

c ++工厂模式语法

[英]c++ factory pattern syntax

In this question , the accepted answer uses the following syntax: 这个问题中 ,接受的答案使用以下语法:

typedef std::map<std::string, Base*(*)()> map_type;

Can someone explain what the (*) means, I've never seen it before? 有人可以解释(*)的意思,我以前从未见过它吗?

It is a function pointer that returns a Base pointer and takes no arguments, eg 它是一个返回Base指针并且不带参数的函数指针 ,例如

struct Base {};

Base* myfun() {
    return 0;
}

int main() {
    std::map<std::string, Base*(*)()> mymap;
    mymap.insert(std::make_pair("hello", myfun));
}

Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM