Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:12:56+00:00 2026-06-09T19:12:56+00:00

Is casting the const ness of member function pointers defined in C++? Is the

  • 0

Is casting the constness of member function pointers defined in C++? Is the following valid code?

struct T {
  void foo(int i) const { std::cout << i << std::endl;};
};

void (T::*f1)(int) const = &T::foo;
void (T::*f2)(int)       = reinterpret_cast<void (T::*)(int)>(f1);
T t;
(t.*f2)(1);

Update:

The reason why I need this is that I’m writing a function that accepts both an object and a member function pointer to that object. I need a version for const objects (accepting only const functions) and a normal one. Since I don’t want duplicate code, my idea was to put the actual code in the non-const version and call it from the const one, casting away any consts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:12:57+00:00Added an answer on June 9, 2026 at 7:12 pm

    Compiler eats it.
    But the backward cast is more useful.

    And again but – it is better to don’t use it, const_cast is usually just a quick and dirty solution, which you apply only when there are not any other solution.

    Answer to update

    If I understand you correctly you are going to use one object and two function. First function accepts const object and const member-function, second – non-const object and non-const member-function.

    According to given information you can change second function to accept non-const object and const member-function. And give them one non-const object and its const member-function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know, casting between incompatible function pointers, eg: void aUnaryFunction(int a1)
static int write_stream(const void *buffer, size_t size, void *app_key) { char *stream = (char
class Base { public: virtual void foo() const { std::cout << Base; } };
I have a piece of C code that looks like this: const char (*foo)[2]
Here is my code: 1 #include <cstdio> 2 3 struct Foo; 4 5 struct
I have a function declared as: int myFunction(const float** ppArr, const int n, const
Consider the below code snippet: int main() { const int i=3; int *ptr; ptr=const_cast<int*>(&i);
The naming convention for constants in C# is Pascal casing: private const int TheAnswer
I have the following casting problem when my data structure sSpecificData contains a field
I usually use C type casting in C/C++ code. My question is, does adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.