Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I was just working with function pointers and I remembered that you could do this:

void Foo()
{
}

int main()
{
    void(& func)() = Foo;

    func(); //::Foo();
}

The obvious advantage being that references reference valid objects (unless they're misused), or functions in this case.

The obvious disadvantages being that you can't store an array of references and can't use them for member function pointers (at least as far as I can tell).

My question: does anyone use them (i.e., function references, not function pointers), and if so, in what scenarios have you found them useful/helpful?

The only place I can see them being useful off the bat is binding a reference to a certain function when working with conditional compilation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
222 views
Welcome To Ask or Share your Answers For Others

1 Answer

I've used them before to add customization to classes by passing them to the constructor in a way like the strategy pattern


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...