Point to Class members

Source: Internet
Author: User
Tags bool

Summary: This article describes the internal mechanism of the class member pointer and how it is generally used ...

Recently I received many questions about the class method pointer, such as: The class method pointer cannot be used in VC, the compiler reported the following error:

Cannot convert parameter 2 from ''long (unsigned long)''to''long (__cdecl *)(unsigned long)''

What am I going to do?

Here is the code to solve the problem:

//in the header
class CKernel:
{
  long (*lpFunc)(DWORD);
  long OLESendTC( DWORD dwInfo );
}
//in the cpp File
BOOL CKernel::Init()
{
  lpFunc = OLESendTC;
  return TRUE;
}

As you know, class members have an implied parameter, a pointer to the class object itself, and the method is invoked on this object. C + + uses this pointer to find the location of the class data that any method intends to reference. If you want to try to invoke a method of a class using a standard function pointer, C + + cannot pass this implied parameter and cause a conflict.

To address this problem and improve the type safety mechanism, C + + added three new operators::*,. * And, to introduce a secure member pointer. These member pointers can either point to a member function or point to a variable.

class CTest
{
public:
  BOOL Init();
  long OLESendTC(DWORD dwInfo);
};
long (CTest::*lpFunc)(DWORD dwInfo) = &CTest::OLESendTC;
int main()
{
  CTest test;
  (test.*lpFunc)(0);
  return 0;
}
long CTest::OLESendTC(DWORD dwInfo)
{
  cout << "IN OLESENDTC\n";
  return 0;
}

This example demonstrates the use of the member pointer. Code uses::* operator to declare Lpfunc as a pointer to a CTEST member function. Note that this is not assigned to this pointer at run time, and the pointer is initialized in the declaration. In the main function, this example uses the. * operator to invoke the method pointed by Lpfunc. If test is a pointer here, you will replace it with the->* operator.

C + + has many internal mechanisms, such as hiding method parameters. The member pointer enables you to safely declare a class method pointer and invoke the method with that pointer.

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.