C + + Debugging methods for tracking class member variables _c language

Source: Internet
Author: User

For example: Int (*foo) (int arg), remember to distinguish it from another pointer function, like this: int *foo (int arg).
For example, we can declare a variable and a function like this:

Copy Code code as follows:

Int (*pfun) (int arg) = 0;
int fun (int arg); This function is implemented casually, I will not write.

If we want to take advantage of the function pointer manipulation function, we will use the same as the pointer variable:

Copy Code code as follows:

Pfun=fun;
int result= (*pfun) (123);

Yes, it's not necessary to have a chicken ribs. This is, of course, because we are useless in the right place. Here's what I'm going to talk about is using a class to call a member of another unrelated class.

Code:

Copy Code code as follows:

#include <iostream>
using namespace Std;
Template<typename T,typename n>
Class functor{
Public
Functor (t *otherp,n (t::* otherfun) (N Arg))
{
Mp=otherp;
Mfun=otherfun;
}
Virtual n operator () (n Arg)
{
Return (*mp.*mfun) (ARG);
}
Private
N (T::* mfun) (n Arg);
T *mp;
};
Class a{
Public
A (int A0): A (A0) {}
int traced (int b)
{
cout<< "Trace a=" <<a<< ", b=" <<b<<endl;
return 0;
}
Private
int A;
};
int main ()
{
A A (10);
Functor<a,int> Trace (&a,&a::traced);
Trace (5);
return 0;
}

Line 33rd passes the member function address of Class A to the functor function pointer, which enables the members of a functor to be processed through the members of a.
Here we use an overload of operator (), which can be replaced by another function to handle the functor function pointer.
(It doesn't work, but the function pointer is very human, not intuitive), like this:

Copy Code code as follows:

#include <iostream>
using namespace Std;
Template<typename T,typename n>
Class functor{
Public
Functor (t *otherp,n (t::* otherfun) (N Arg))
{
Mp=otherp;
Mfun=otherfun;
}
Virtual n Out (n arg)//change
{
Return (*mp.*mfun) (ARG);
}
Private
N (T::* mfun) (n Arg);
T *mp;
};
Class a{
Public
A (int A0): A (A0) {}
int traced (int b)
{
cout<< "Trace a=" <<a<< ", b=" <<b<<endl;
return 0;
}
Private
int A;
};
int main ()
{
A A (10);
Functor<a,int> Trace (&a,&a::traced);
Trace.out (5); Changes
return 0;
}

C + + is really complicated, but if we use it well, complexity is powerful.

Related Article

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.