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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:46:14+00:00 2026-05-15T17:46:14+00:00

In book named Using C++ by Rob McGregor there is following example of using

  • 0

In book named “Using C++” by Rob McGregor there is following example of using pointer-to-member operator

class mycls
{
public:
    int member;
    int *ptr;
};

void main()
{
    mycls MyClass;

    // Derive a pointer type to the non-pointer class member
    int mycls::*member = &mycls::member;

    MyClass.ptr = new int;
    mycls* pMyClass = &MyClass;

    pMyClass->*member = 5;
    *MyClass.ptr = 10;

    cout << "pMyClass->*member = " << pMyClass->*member << "\n"; // pMyClass->*member = 5
    cout << "MyClass.*member = " << MyClass.*member << "\n"; // MyClass.*member = 5
    cout << "*MyClass.ptr = " << *MyClass.ptr << "\n"; // *MyClass.ptr = 10
    cout << "*pMyClass->ptr = " << *pMyClass->ptr << "\n"; // *pMyClass->ptr = 10

    delete MyClass.ptr;
}

In this example I don’t understand why member variable mycls::member becomes maybe a pointer after (guessing) this line of code:

int mycls::*member = &mycls::member; 

What this does?

  • 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-15T17:46:15+00:00Added an answer on May 15, 2026 at 5:46 pm

    Suppose you had a local variable:

    int member;
    

    You could make a pointer to it with:

    int *ptr = &member;
    

    To get the pointer to member syntax, we just append mycls:: in the appropriate places:

    int mycls::*member = &mycls::member;
    

    It might be clearer with an example that shows how the pointer can switch between any members of the class that are of the correct type:

    class C
    {
    public:
        int a;
        int b;
    };
    
    void main()
    {
        // make pointer to member, initially pointing to a
        int C::*ptrToMember = &C::a;
    
        C x;
        C *ptrToObj = &x; // make pointer to object x
    
        ptrToObj->*ptrToMember = 2; // store in a;
    
        ptrToMember = &C::b; // change pointer to b
    
        ptrToObj->*ptrToMember = 3; // store in b;
    }
    

    Note how we create the pointer to the member a before we’ve created an object of type C. It’s only a pointer to a member, not a pointer to the member of a specific object. In the ‘store’ steps, we have to say which object as well as which member.

    Update

    In the comments the OP asked if this is the same:

    int *ptr = &(ptrToObj->a);
    

    No, it’s not. That is a pointer to any int, anywhere in memory.

    The easiest way to understand this is to think of what it means technically. A “pointer” is an absolute location in memory: where to find an object. A “pointer-to-member” is a relative location, sometimes called an offset: where to find an object within the storage of an outer object. Internally they are just numbers. A pointer-to-member has to be added to an ordinary pointer to make another pointer.

    So if you have a pointer to an object (an int is an object!), you can use it to change what is stored at that absolute location in memory:

    *ptr = 123;
    

    But if you have a pointer-to-member, it is not a memory location. It is an offset, an amount to be added to a memory location. You cannot use it by itself. You must “add” it to an object pointer:

    ptrToObj->*ptrToMember = 132;
    

    This means: go to the location in memory ptrToObj, then move along by the distance ptrToMember.

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

Sidebar

Related Questions

I have an example class book : public class Book { [Key] public int
There are a group of entities named Book and Magazine which inherits from the
I have one grails application.In that I have one model class named Book. From
I am using Rails tutorial with example book by Michael Hartl as a reference
I have the following XML in a string named 'xml': <?xml version=1.0 encoding=ISO-8859-1?> <Book>
For example,I want to generate the following xml file by using java with DOM
I have the following models: class Author(models.Model): author_name = models.CharField() class Book(models.Model): book_name =
I using the project named coredatabooks from apple developer examples. #import @interface Book :
I've tried to implement the example from the book named OpenCV 2 Computer Vision
From the book Agile Web Development With Rails class Order < ActiveRecord::Base named_scope :last_n_days,

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.