The header file contains different sequences, causing an exception in the member function pointer definition.

Source: Internet
Author: User

I think this is a bug in the VC compiler. The inclusion sequence of header files should not cause exceptions during runtime. The same problem exists in both vs2008 and vc6, but everything works normally in C-free (using the GCC compiler.

The following is an example code that generates an exception in VC. Note the inclusion sequence of the header file.

Two classes classa and classb are defined. A member function pointer of classa is defined in classb. H, and classa is pre-declared.

//// ClassA.h//class ClassA{public: void Func(void) {} void TestA(void);};

 

/// Classb. h /// pre-declaration classaclass classa; typedef void (classa: * pmemfunc) (void); Class classb {public: classb (void); void testb (classa * Pclass, pmemfunc); Private: pmemfunc m_pmemfunc; // defines the member function pointer of classa };

 

/// Classa. CPP // # include "classa. H "// when classa. H is included in classb. an exception occurred before h # include "classb. H "// If classa. H is included in classb. no problem after H // # include "classa. H "Void classa: TESTA (void) {pmemfunc = classa: func; this-> func (); // output in VC: 4, output in GCC: 8 STD: cout <"pmemfunc_size =" <sizeof (pmemfunc) <STD: Endl; classb objb; // when running here, the this pointer of classa is changed, causing an exception objb. testb (this, pmemfunc );}

 

/// Classb. CPP // # include "stdafx. H "# include" classb. H "classb: classb (void): m_pmemfunc (null) {} void classb: testb (classa * Pclass, pmemfunc) {// output in VC: 16, output in GCC: 8 STD: cout <"pmemfunc_size =" <sizeof (pmemfunc) <STD: Endl ;}

 

The younger brother cannot solve the above exceptions.

The following information is obtained from the Internet:

The member function pointer is different from the general pointer, but it may occupy a larger memory space than the general pointer. In VC, a member function pointer can be 4, 8, 12, or even 16 bytes long, depending on the nature of the related classes, it also depends on the Compilation settings used by the compiler. In the case of single inheritance, multi-inheritance, and virtual inheritance, the size of the member function pointer is 4 bytes, 8 bytes, and 12 bytes respectively. If you do not know whether a class inherits (this class only has a pre-declaration but does not provide actual definitions), its member function pointer can occupy a maximum of 16 bytes, it is larger than 4 bytes of the normal pointer.

In the process of C ++ standardization, there was a heated debate on the implementation of member function pointers. Before the Standards Committee made a decision, different compiler manufacturers have implemented their own compilers based on their different answers to these questions.

Reference link:

Member function pointers and high-performance C ++ Delegation
Http://wenku.baidu.com/view/3a6744c7d5bbfd0a795673a3.html

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.