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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:35:18+00:00 2026-06-09T12:35:18+00:00

I write some testing code, to figure out the address about member function. But

  • 0

I write some testing code, to figure out the address about member function. But the result confuse me. the code is

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

class A
{
public:
        A(char _a, char _b, char _c, char _t):a(_a), b(_b), c(_c), t(_t){}

private:
        char t;
        char a;
public:
        char b;
        char c;
        void test1(){std::cout << a << std::endl;}
        void test2(){std::cout << b << std::endl;}
        void test3(){std::cout << c << std::endl;}
        void test4(){std::cout << t << std::endl;}

        static void print(){

                char A::*pa = &A::a;
                char A::*pb = &A::b;
                char A::*pc = &A::c;
                char A::*pt = &A::t;
                printf("data member : %p, %p, %p, %p\n", pa, pb, pc, pt);

                void (A::*pf1)() = &A::test3;
                void (A::*pf2)() = &A::test2;
                void (A::*pf3)() = &A::test3;
                void (A::*pf4)() = &A::test2;
                printf("function member : %p, %p, %p, %p\n", pf1, pf2, pf3, pf4 );
        }
};

int main()
{
        A::print();

    //system("pause");//need on Dev-c++
        return 0;
}

I run this code on ubuntu with g++, and Windos with Dev-C++, the results are the same

data member : 0x1, 0x2, 0x3, (nil)
function member : 0x8048780, (nil), 0x804874e, (nil)

Why the pf2 and pf4 is nil?

  • 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-09T12:35:19+00:00Added an answer on June 9, 2026 at 12:35 pm

    Pointers to members are not regular pointers, you can test it by printing the size of the pointer to member and the size of a regular pointer. The code that you have exhibits undefined behavior, but if you want a simple explanation of the results, printf takes a variable number of arguments, which is implementing by pushing the arguments to the stack in some preconceived order and adding some extra information on what the number of arguments in the call is.

    In your case, the pointer-to-member function are being pushed as they are (usually twice the size of a regular pointer), and then printf is reading the stack and interpreting the bits there as regular pointers. My guess (you would have to read the compiler/platform documentation to verify this) is that you are only printing the first two arguments, with the contents separated by commas.

    A slightly better way of doing that would be to create a union of a pointer to member function and a char array of the appropriate size, initialize it with the pointer to member and print the hexadecimal values through the array.

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

Sidebar

Related Questions

I am starting to write some code for A/B testing in a Grails web
I am trying to write a wrapper function to figure out who is calling
I am testing the behaviour of some client software, and need to write some
i write some utility class but how to get name ? Can that send
I am trying to write some simple code which will read a text file
I am attempting to write some code that will expediently process video frames. I
I am writing some testing software that receives some source code, compiles it on
Due to testing and time measuring I have to write some kind of log
I write code primarily for personal use, but I'm considering releasing an application (scientific
I was testing some code on the tryit editor and was unable to replicate

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.