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

    [RESOLVED] Why won't this pointer-to-member-function compile?

    Hello:

    Given that the following two examples compile without any errors:
    Code:
    class Tester
    {
        bool FunctionA(int) { return true; }
        bool FunctionA(int, int, int) { return true; }
        bool FunctionB(int) { return true; }
        bool FunctionB(int, int, int) { return true; }
     
        void Member(void)
        {
            int static const iVariationA = 0;
            int static const iVariationB = 1;
            int static const nVariations = 2;
     
            for (int i = 0; i < nVariations; ++i)
            {
                bool (Tester::* const pfn)(int) = &Tester::FunctionA;
            }
        }
    };
    void main(void)
    {
        Tester tester;
    }
    Code:
    class Tester
    {
        bool FunctionA(int) { return true; }
        bool FunctionA(int, int, int) { return true; }
        bool FunctionB(int) { return true; }
        bool FunctionB(int, int, int) { return true; }
     
        void Member(void)
        {
            int static const iVariationA = 0;
            int static const iVariationB = 1;
            int static const nVariations = 2;
     
            bool (Tester::* static const pfnVariations[nVariations])(int)
                = { &Tester::FunctionA, &Tester::FunctionB };
     
            for (int i = 0; i < nVariations; ++i)
            {
                bool (Tester::* const pfn)(int) = pfnVariations[i];
            }
        }
    };
    void main(void)
    {
        Tester tester;
    }
    Does anyone know why this (which I would prefer instead of the workaround in the last example) won't compile?:

    Code:
    class Tester
    {
        bool FunctionA(int          ) { return true; }
        bool FunctionA(int, int, int) { return true; }
        bool FunctionB(int          ) { return true; }
        bool FunctionB(int, int, int) { return true; }
     
        void Member(void)
        {
            int static const iVariationA = 0;
            int static const iVariationB = 1;
            int static const nVariations = 2;
     
            for (int i = 0; i < nVariations; ++i)
            {
                bool (Tester::* const pfn)(int) = ((i == iVariationA) ? (&Tester::FunctionA)
                                                                      : (&Tester::FunctionB));
            }
        }
    };
    void main(void)
    {
        Tester tester;
    }
    1>test.cpp(124) : error C2568: ':' : unable to resolve function overload
    1> test.cpp(111): could be 'bool Tester::FunctionA(int,int,int)'
    1> test.cpp(110): or 'bool Tester::FunctionA(int)'
    Thanks

  2. #2
    Join Date
    Mar 2006
    Posts
    151

    Re: Why won't this pointer-to-member-function compile?

    I've just figured it out (sort of). Apparently the type information or function signature is lost in the tertiary operator, as if the choice between the two overloads is made apart from the type of the variable to which it will be assigned. (I'm sure some C++ grammar guru could state it better.)

    To my surprise, a static cast fixes the issue:
    Code:
                bool (Tester::* const pfn)(int) = ((i == iVariationA)
                    ? (static_cast<bool (Tester::*)(int)>(&Tester::FunctionA))
                    : (static_cast<bool (Tester::*)(int)>(&Tester::FunctionB)));

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