How to output a class's function address __ function

Source: Internet
Author: User

First we define a class CTest, which contains three different forms of member functions, static member functions Statfunc (), dynamic member functions Dynfunc (), and virtual function virtfunc (). In the main function, we use the cout standard output stream to output the addresses of these three functions separately, as shown in the following procedure:

#include <iostream>
#include <stdio.h>
using namespace Std;

Class CTest
{
Public
static void Statfunc ()
{cout << "Statfunc" << Endl}

void Dynfunc ()
{cout << "Dynfunc" << Endl}

virtual void Virtfunc ()
{cout << "Virtfunc" << Endl}
};

void Main ()
{
cout << "Address of Ctest::statfunc:" << &ctest::statfunc << Endl;
cout << "Address of CTest::d ynfunc:" << &ctest::d ynfunc << Endl;
cout << "Address of Ctest::virtfunc:" << &ctest::virtfunc << Endl;
while (1);
}

The results of the screen output are shown in the following illustration:


You can see from the diagram that the static function's address appears normal, is a 32-bit address value, but the dynamic function and the virtual function address both output 1, obviously not the address value.

In order to know why, we have to analyze the difference in the operating mechanism of these kinds of member functions. We all know that static functions are independent of objects and are owned by classes, so we call static functions that can be invoked either through class calls (such as Ctest::statfunc ()) or through object invocation (such as CTest object; Object.statfunc ()). Whether through a class call or an object invocation, the corresponding function is the same.

      However, for dynamic functions, they can only be invoked through objects. Because we're in a dynamic member function, it is often necessary to access the members of the object, we know the different objects of the same type, they have different copies of the member variables in the class, so if a dynamic member function is called by a class, how do we know which object's member variable is accessed in the function? In addition, we say that a call to a dynamic function must be called by an object. Does it mean that dynamic member functions are bound to objects, and that the member functions invoked by different objects are different? (note, we say that the member function is different, that is, the function code stores the address is different, not to say that the function of the different behavior), So we want to output the address of the dynamic function, we must get it through the object, such as CTest object; &object.statfunc, we compile on the compiler and we can discover programming errors because objects can only be used to invoke functions and cannot be used to get the address of a function. Because we want to get the function address, we have to get it through the class, and the dynamic function is bound to the class rather than to the object. When C + + calls Non-static member functions, it uses a __thiscall method of function invocation. In this way, the compiler, at compile time, adds a pointer to the call to the function parameter list, which we often call the this pointer. The form of the call is similar to the CTest::d ynfunc (ctest* this, otherparam ...), in the function body, involving the object's member variables or other member functions, will be called through this pointer, This will result in the processing of the data of the calling object in the member function without error processing the data of the other object. Obviously, although we have to invoke the dynamic function through the object, we are actually accessing the same member function. So we use &ctest::d Ynfunc class name to get the member function address is true, dynamic functions are also tied to the class rather than to the object.

The reason for the error is that our output operator << does not overload the Void (__thiscall A::*) () type, the compiler converts this type to bool type, so the output is 1; for static functions, it is not invoked __thiscall,<< There is an overload on it, so the static function of the class can directly output the function address with cout. We can use printf output because he can receive any type of parameter, including the __thiscall type, so we will cout << "address of CTest::d ynfunc:" << &ctest:: Dynfunc << Endl to printf ("Address of CTest::d ynfunc:x\n", &ctest::d ynfunc); the output is shown in the following illustration:


You can read from the diagram, and by using printf output, we get the address of the dynamic function. So for the virtual function, we also use printf to output, is it OK, we will cout << "address of Ctest::virtfunc:" << &ctest::virtfunc < < Endl;
To printf ("Address of ctest::virtfunc:x\n", &ctest::virtfunc), the output of the run is as follows:


It can be seen from the above figure that a quasi address value can also be obtained.

In order to verify the correct address, we can define three member function pointers to save the obtained function address, and then call the function pointer to see if the output is correct, you can determine whether the address is correct. The following code is validated:

#include <iostream>
#include <stdio.h>
using namespace Std;

Class CTest
{
Public
static void Statfunc ()
{
cout << "Statfunc" << Endl;
}

void Dynfunc ()
{
cout << "Dynfunc" << Endl;
}

virtual void Virtfunc ()
{
cout << "Virtfunc" << Endl;
}
};

void Main ()
{
CTest Object;
ctest* pobject = &Object;
cout << "Address of Ctest::statfunc:" << &ctest::statfunc << Endl;
printf ("Address of CTest::d ynfunc:x\n", &ctest::d ynfunc);
printf ("Address of ctest::virtfunc:x\n", &ctest::virtfunc);
static void (*p_statfunc) ();
void (ctest::* p_dynfunc) ()//Note the definition of a Non-static member function pointer needs to be specified in the domain of that class
void (ctest::* P_virtfunc) ();
P_statfunc = &Ctest::statFunc;
P_dynfunc = &ctest::d ynfunc;
P_virtfunc = &Ctest::virtFunc;
P_statfunc ();
The call to a Non-static member function pointer is also different from a normal function, because the. * has a lower priority than (), so you need to use parentheses to put the left action

Number, if written as Object.*p_dynfunc (), it will fail to compile

(Object.*p_dynfunc) ();

(Object.*p_virtfunc) ();
while (1);
}
After the code is run, it appears as follows, from the output we have successfully invoked the corresponding member function:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.