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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:13:18+00:00 2026-06-15T04:13:18+00:00

I have come across the following code but not able to understand what is

  • 0

I have come across the following code but not able to understand what is the importance of the following concept . Please help .

#include <iostream>
using namespace std;

class cl 
{

public:
    cl(int i) { val=i; }
    int val;
    int double_val() { return val+val; }

};

int main()
{
    int cl::*data; // data member pointer
    int (cl::*func)(); // function member pointer
    cl ob1(1), ob2(2); // create objects
    cl *p1, *p2;
    p1 = &ob1; // access objects through a pointer
    p2 = &ob2;
    data = &cl::val; // get offset of val
    func = &cl::double_val; // get offset of double_val()
    cout << "Here are values: ";
    cout << p1->*data << " " << p2->*data << "\n";
    cout << "Here they are doubled: ";

    cout << (p1->*func)() << " ";
    cout << (p2->*func)() << "\n";
    return 0;
}
  • 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-15T04:13:20+00:00Added an answer on June 15, 2026 at 4:13 am

    Pointers-to-members are a special gadget in C++ that tell only tell you about a which member of a class you want without pertaining to any object. You need to combine a pointer-to-member with an object instance in order to obtain an actual member, whether it be a data or a function member.

    Think of pointers-to-member as “offsets”. Also, remember that pointers-to-member are not pointers!

    We can rewrite your code to make it a bit clearer:

    struct Foo
    {
        int a;
        int b;
        double f(int, int);
        double g(int, int);
    };
    
    int main()
    {
        int Foo::*ptm = &Foo::a;
        double (Foo::*ptmf)(int, int) = &Foo::f;
    
        Foo x, y, z;
    
        int numbers[] = { x.*ptm, y.*ptm, z.*ptm }; // array of Foo::a's
    
        ptm = &Foo::b;                              // reseat the PTM
    
        int more[] = { x.*ptm, y.*ptm, z.*ptm };    // array of Foo::b's
    
    
        double yetmore[] = { (x.*ptmf)(1,2), (y.*ptmf)(1,2) };
    
        ptmf = &Foo::g;
    
        double evenmore[] = { (x.*ptmf)(1,2), (y.*ptmf)(1,2) };
    }
    

    Note how we’re using the pointers-to-member to refer only to the class member, and we can use that to get the same relative class member out of different objects.

    (By contrast, we can also form a real pointer to an actual object member: int * p = &x.a;. That’s because the member-int is actually an integer. But we cannot do anything analogous for member functions, because member functions are not functions (e.g. you cannot call them).)

    Make sure you understand the difference between classes and objects, and this should become very clear!

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

Sidebar

Related Questions

I have come across the following jQuery code but could not understand it. What
I have come across the following link: http://code.google.com/p/a-star/source/browse/trunk/java/PathFinder.java?r=8 I have got the code working
I need to do the following .. have come across various examples but i
I have been following a tutorial to better understand php's oop, but have come
I have come across the following basic Tree conflict: Local add, incoming add upon
I am writing an API and have come across the following pattern: My API
I have come across scripts that use: isset($_POST['submit']) as well as code that uses:
I have come across loop-unrolling but what other types of compiler optimization are there
I have come across something very similar to the following when looking for basic,
I'm working with the EXIF library at http://code.google.com/p/iphone-exif/ and I have come across a

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.