深奥的C ++运算符 [英] Esoteric C++ operators

查看:84
本文介绍了深奥的C ++运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下深奥的C ++运算符的作用是什么?

What is the purpose of the following esoteric C++ operators?

指向成员的指针

::*

按指针将指针绑定到成员

Bind pointer to member by pointer

->*

通过引用绑定指向成员的指针

Bind pointer to member by reference

.*

(参考)

推荐答案

指向成员的指针使您可以拥有一个与特定类相关的指针.

A pointer to a member allows you to have a pointer that is relative to a specific class.

所以,假设您有一个包含多个电话号码的联系人课程.

So, let's say you have a contact class with multiple phone numbers.

class contact
{
    phonenumber office;
    phonenumber home;
    phonenumber cell;
};

这个想法是,如果您有一个需要使用电话号码的算法,但决定应该在算法之外使用哪个电话号码,则指向成员的指针可以解决此问题:

The idea is if you have an algorithm that needs to use a phone number but the decision of which phone number should be done outside the algorithm, pointers to member solve the problem:

void robocall(phonenumber contact::*number, ...);

现在,robocall的呼叫者可以确定要使用的电话号码类型:

Now the caller of robocall can decide which type of phonenumber to use:

robocall(&contact::home, ...);    // call home numbers
robocall(&contact::office, ...);  // call office number

一旦有了指针,

.*->*就会起作用.因此,在robocall内部,您可以这样做:

.* and ->* come into play once you have a pointer. So inside robocall, you would do:

contact c = ...;
c.*number;    // gets the appropriate phone number of the object

或:

contact *pc = ...;
pc->*number;

这篇关于深奥的C ++运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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