Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Declare a Method Pointer That's Constant

1st off this isn't a question about how to point to a constant method. I want to know how to make my method pointer constant.

Given:

struct foo {
    void func1();
    void func2();
};

I can construct a method pointer with void (foo::*bar)() = &foo::func1 but I can later do bar = &foo.func2 and I want to prevent that.

I can easily do this with const auto bar = &foo::func1, but I'm not sure how to do this pre c++11.

like image 953
Jonathan Mee Avatar asked Mar 04 '23 03:03

Jonathan Mee


1 Answers

All you have to do is to add the const keyword after the *, like this:

void(foo::*const _pointer)() = &foo::func1;
like image 147
terrakuh Avatar answered Apr 01 '23 16:04

terrakuh