我从未见过的std :: vector的新语法.帮助? [英] new syntax for std::vector i've never seen before. help?

查看:112
本文介绍了我从未见过的std :: vector的新语法.帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了一些各种文档和论坛,而不是有趣的做事方式,而且我偶然发现了一种我从未见过的语法.有人可以告诉我这是什么意思吗?

i''ve just been reading some various documentation and forums and what not for interesting ways of doing things, and i''ve stumbled upon a syntax i''ve straight up never seen before. can someone tell me what the heck this means?

std::vector <base *(*)()>  vectorArray;



我在很多程序中都使用过向量,但是我以前从未见过这样的事情.显然,它允许您在其中存储函数调用,然后根据该函数调用所在的任何迭代器,您可以进行以下操作:



i''ve been using vectors in many of my programs, but i''ve never seen something like this done before. apparently it allows you store a function call in it, and then, based on whatever iterator that function call is at, you just go:

vectorArray[0] ();



它会调用您放置在其中的任何函数.这似乎是一种为各种派生类调用构造函数的绝妙方法,因此您可以使迭代器与某个类同义,甚至使用map映射:P,但可以使用它为派生类调用构造函数并将其存储为指向基类的指针,但是语法对我来说太陌生了,我想知道是否可以安全使用它.我真的很想:P所以,总而言之,我的问题是,这是做什么的,它是如何做的,此操作是特定于矢量的还是c和c ++数组的常规操作,使用它是否安全? ?



and it calls whatever function you put there. This seems like an excellent way to call constructors for various derived classes, so you can just make an iterator synonymous with a certain class, shoot use a map even :P, but use it to call the constructor for a derived class and store it an a pointer to the base class, but the syntax is so foreign to me, i''m tentative as to whether it''s safe to use it or not. i''d really like to :P so, in summary, my question is, what is this doing, how is it doing it, is this operation specific to vectors or general to c and c++ arrays, and is it safe to use it?

推荐答案

base *(*)()-指向返回"base *"类型的函数的指针.因此,vectorArray可以将base * Fun()类型的函数作为元素.
base *(*)() - pointer to a function which returns a type ''base*''. So, vectorArray can take functions of type base* Fun() as its elements.


base *(*)()


是指向函数的指针的语法,该函数返回指向base的指针;因此您的向量将包含指向函数的指针.


is the syntax for a pointer to a function which returns a pointer to base; so your vector will contain pointers to functions.


这篇关于我从未见过的std :: vector的新语法.帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆