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 6195889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:32:55+00:00 2026-05-24T03:32:55+00:00

Suppose that we have a pointer-to-class member pointing at a field of a class.

  • 0

Suppose that we have a pointer-to-class member pointing at a field of a class. We also have a pointer to that specific field in a particular instance of the class. For example, we might have something like this:

class A {
     B inner_object;
}

A* myA = /* ... */
B* ptr = &myA->inner_object;
B A::* memPtr = &A::inner_object;

Is there a way to use ptr and memPtr to recover myA? That is, if we didn’t already have an explicit pointer for myA, could we make one out of ptr and memPtr?

  • 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-05-24T03:32:56+00:00Added an answer on May 24, 2026 at 3:32 am

    After a decent amount of research…

    This is actually done in most industrial intrusive list implementations. It does require some hackery, however.

    Boost intrusive structures use the following (and yes, it is implementation specific)

    template<class Parent, class Member>
    inline const Parent *parent_from_member(const Member *member, const Member Parent::*   ptr_to_member)
    {
       return (const Parent*)((const char*)member -
          offset_from_pointer_to_member(ptr_to_member));
    }
    
    
    template<class Parent, class Member>
    inline std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member)
    {
       //The implementation of a pointer to member is compiler dependent.
       #if defined(BOOST_INTRUSIVE_MSVC_COMPLIANT_PTR_TO_MEMBER)
       //msvc compliant compilers use their the first 32 bits as offset (even in 64 bit mode)
       return *(const boost::int32_t*)(void*)&ptr_to_member;
       //This works with gcc, msvc, ac++, ibmcpp
       #elif defined(__GNUC__)   || defined(__HP_aCC) || defined(BOOST_INTEL) || \
         defined(__IBMCPP__) || defined(__DECCXX)
       const Parent * const parent = 0;
       const char *const member = reinterpret_cast<const char*>(&(parent->*ptr_to_member));
       return std::ptrdiff_t(member - reinterpret_cast<const char*>(parent));
       #else
       //This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC
       return (*(const std::ptrdiff_t*)(void*)&ptr_to_member) - 1;
       #endif
    }
    

    Essentially the same thing (albeit in C) as is done in the linux kernel to manage intrusive lists, with the container_of macro (but of course ptr-to-members are not used):

    #define container_of(ptr, type, member) ({ \
                const typeof( ((type *)0)->member ) *__mptr = (ptr); 
                (type *)( (char *)__mptr - offsetof(type,member) );})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a managed class that converts a delegate member to a native
Suppose that I have an integer variable in a class, and this variable may
I have a pointer to a given class. Lets say, for example, the pointer
Suppose that we have a class class Abstract { Abstract( void ); virtual ~Abstract(
Suppose I have two instances of the same class. The class has a pointer
Let's suppose I have an instance of the TList class (BDS 2006, so this
Suppose that I have a database with student information: {'student_name' : 'Alen', 'subjects' :
Suppose that you have a function f: List a -> a such that f
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME

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.