Why doesn't C++ have a pointer to member function type?

后端 未结 9 1840
一生所求
一生所求 2020-12-30 07:24

I could be totally wrong here, but as I understand it, C++ doesn\'t really have a native \"pointer to member function\" type. I know you can do tricks with Boost and mem_fu

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 08:27

    It occurs to me that methods have an implicit this argument, so a c pointer to a method is insufficient to allow the method to be called (because there is no way to determine which instance should be used for this (or even if any instance is currently extant)).

    Edit: Rocketmagnet comments that he addressed this in the question, and that appears to be the case, though I think that was added after I started this response. but I'll say "mea culpa", anyway.

    So allow me to expand on the thought a bit.

    C++ is closely related to c, and has all its intrinsic types compatible with the earlier language (largely because of the history of c++ development, I suppose). So, a intrinsic c++ pointer is a c pointer, and is incapable of supporting the use you ask for.

    Certainly you could build a derived type to do the job---as in the boost implementation---but such a critter belongs in a library.

提交回复
热议问题