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

  • Home
  • SEARCH
  • 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 6638939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:29:45+00:00 2026-05-25T23:29:45+00:00

#include <iostream> using namespace std; typedef void (*pFun)(void); class A { private: int a;

  • 0
#include <iostream>
using namespace std;
typedef void (*pFun)(void);
class A
{
private:    
    int a; 
    int b;
    virtual void outPrint()
    {
        cout << b << endl;
    }
 public:
    A()
    {
        a = 3;      
        b = 4;
    }
};

int main() 
{   
    A obj;
    cout << *((int*)(&obj)+1) << endl;
    pFun pFunc;     
    pFunc = (pFun)*((int*)*(int*)(&obj));   
    pFunc();
    system("pause");
    return 0; 
}

when i call pFunc(), the result i think should be 4,but actually it’s a random number.
and i debug the program, finding that pFunc points to the outPrint function. i don’t know why,plz help me

  • 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-25T23:29:46+00:00Added an answer on May 25, 2026 at 11:29 pm

    Seems that you assume that the address of the object obj can be interpreted as an address of a function of the type you defined.

    It cannot.

    I’m not entirely sure why would you have such an assumption to begin with. You’re probably assuming things about the internal implementation of the dynamic dispatch. But do take a note: A::outPrint that you expect to be called, accepts 1 parameter (this, that is not explicitly defined), whereas your typedef assumes no parameters. So even if the address casting works fine and the address you get is that of the member function (which is a stretch to begin with), your call is incorrect.

    If you change the typedef to:

    typedef void (*pFun)(A*);
    

    and the call to

    pFunc(&obj);
    

    That might work. Yet, the behavior per spec is undefined, to the best of my understanding.

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

Sidebar

Related Questions

#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <boost/bind.hpp> #include <iostream> using namespace std; using boost::bind; class A { public: void
#include <iostream> #include <fstream> #include <cstring> using namespace std; typedef unsigned long int WORD;
#include <iostream> using namespace std; typedef int MYINT; int main() { int y =
#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> using namespace std; // This first class contains a vector and a
#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {

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.