CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2006
    Posts
    151

    [RESOLVED] pointer-to-member array crashes with virtual inheritance

    Hi,

    I've got the following code which demonstrates a problem and am hoping someone can explain:

    Code:
    struct A
    {
        double x[2];
        double i;
    };
     
    struct B : virtual public A
    {
        double y[2];
    };
     
    int main(void)
    {
        B test;
        test.x[0] = 300;
        test.i    = -5;
     
        A const * const pA = &test;
        B const * const pB = &test;
        bool      const b  = reinterpret_cast<int const *>(pA)
                          != reinterpret_cast<int const *>(pB); // As expected
     
        double (B::*pmemberBBi)    = &B::i;
        double (B::*pmemberBBx)[2] = &B::x;
        double (A::*pmemberABx)[2] = &B::x;
     
        double const * const pBBi_B = &(pB->*pmemberBBi); // Valid pointer even though pointer to derived
        double const * const pBB_B  = pB->*pmemberBBx   ; // Null pointer, but why if the previous line worked?
        double const * const pAB_B  = pB->*pmemberABx   ; // Works (pointer to base)
     
        double bbi_b = pB->*pmemberBBi    ; // Works fine even though it is pointer to derived
        double bb_b  = (pB->*pmemberBBx)[0]; // Crash, but why if the previous line worked?
        double ab_b  = (pB->*pmemberABx)[0]; // Works (pointer to base)
     
        return 0;
    }
    I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?

    Thanks,
    GeoRanger

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: pointer-to-member array crashes with virtual inheritance

    Doesn't compile here:

    http://ideone.com/TbDZg2

    Regards,

    Paul McKenzie

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: pointer-to-member array crashes with virtual inheritance

    Compiles and runs without any errors reported under VS2013 & VS2012. Just because it runs without a crash doesn't mean the program is correct though.

    What compiler are you using?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: pointer-to-member array crashes with virtual inheritance

    From 4.11.2 of the ANSI standard:

    An rvalue of type “pointer to member of B of type cv T,” where B is a class type, can be converted to an
    rvalue of type “pointer to member of D of type cv T,” where D is a derived class (clause 10) of B. If B is an
    inaccessible (clause 11), ambiguous (10.2) or virtual (10.1) base class of D, a program that necessitates this
    conversion is ill-formed.
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2006
    Posts
    151

    Re: pointer-to-member array crashes with virtual inheritance

    Thank you both for your interest and responses.

    Thank you Paul McKenzie for the quote from the Standard. That's the kind of information I was hoping to find.

    2kaud, I'm using Visual Studio Premium 2012. It compiles with no errors or warnings even at Warning Level 4 (except for several "local variable is initialized but not referenced" warnings). What a trap! Intuitively, it seems like it should work.

    Thanks,
    GeoRanger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured