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

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c++ - Function References

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...