Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer to class member function

Pointer to class member function

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mila025
    wrote on last edited by
    #1

    Hi, Maybe today isn't my best day ;) but could anyone tell me what is wrong with this snippet ?

    class CTest
    {
    public:
    CTest()
    {
    (*(this)).lpFunc = &CTest::Write;
    }
    long Write(DWORD dwInfo)
    {
    ...
    }
    long (*lpFunc)(DWORD);
    }

    I've got error C2440:

    '=' : cannot convert from 'long (__thiscall CTest::* )(DWORD)' to 'long (__cdecl *)(DWORD)'

    and when I change line: (*(this)).lpFunc = &CTest::Write; to

    (CTest::*lpFunc) = &CTest::Write;

    I've got another error code:

    error C2059: syntax error : '<tag>::*'

    Generally I want to point on lpFunc a pointer to Write fuction but within class (I don't want to any globals). ----------- Mila -- modified at 3:15 Wednesday 28th June, 2006

    T D J 3 Replies Last reply
    0
    • M Mila025

      Hi, Maybe today isn't my best day ;) but could anyone tell me what is wrong with this snippet ?

      class CTest
      {
      public:
      CTest()
      {
      (*(this)).lpFunc = &CTest::Write;
      }
      long Write(DWORD dwInfo)
      {
      ...
      }
      long (*lpFunc)(DWORD);
      }

      I've got error C2440:

      '=' : cannot convert from 'long (__thiscall CTest::* )(DWORD)' to 'long (__cdecl *)(DWORD)'

      and when I change line: (*(this)).lpFunc = &CTest::Write; to

      (CTest::*lpFunc) = &CTest::Write;

      I've got another error code:

      error C2059: syntax error : '<tag>::*'

      Generally I want to point on lpFunc a pointer to Write fuction but within class (I don't want to any globals). ----------- Mila -- modified at 3:15 Wednesday 28th June, 2006

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      Mila025 wrote:

      '=' : cannot convert from 'long (__thiscall CTest::* )(DWORD)' to 'long (__cdecl *)(DWORD)'

      notice the calling convention of the two parts of your operator = reported by the compiler. you function Write has to be either static or global. the fact is that being a class member function implicitely adds it the this pointer parameter...


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      T 1 Reply Last reply
      0
      • M Mila025

        Hi, Maybe today isn't my best day ;) but could anyone tell me what is wrong with this snippet ?

        class CTest
        {
        public:
        CTest()
        {
        (*(this)).lpFunc = &CTest::Write;
        }
        long Write(DWORD dwInfo)
        {
        ...
        }
        long (*lpFunc)(DWORD);
        }

        I've got error C2440:

        '=' : cannot convert from 'long (__thiscall CTest::* )(DWORD)' to 'long (__cdecl *)(DWORD)'

        and when I change line: (*(this)).lpFunc = &CTest::Write; to

        (CTest::*lpFunc) = &CTest::Write;

        I've got another error code:

        error C2059: syntax error : '<tag>::*'

        Generally I want to point on lpFunc a pointer to Write fuction but within class (I don't want to any globals). ----------- Mila -- modified at 3:15 Wednesday 28th June, 2006

        D Offline
        D Offline
        Don Box
        wrote on last edited by
        #3

        That Write method MUST be static. So solution is :- class CTest { public: CTest() { (*(this)).lpFunc = &CTest::Write; } static long Write(DWORD dwInfo) { ... } long (*lpFunc)(DWORD); }

        1 Reply Last reply
        0
        • M Mila025

          Hi, Maybe today isn't my best day ;) but could anyone tell me what is wrong with this snippet ?

          class CTest
          {
          public:
          CTest()
          {
          (*(this)).lpFunc = &CTest::Write;
          }
          long Write(DWORD dwInfo)
          {
          ...
          }
          long (*lpFunc)(DWORD);
          }

          I've got error C2440:

          '=' : cannot convert from 'long (__thiscall CTest::* )(DWORD)' to 'long (__cdecl *)(DWORD)'

          and when I change line: (*(this)).lpFunc = &CTest::Write; to

          (CTest::*lpFunc) = &CTest::Write;

          I've got another error code:

          error C2059: syntax error : '<tag>::*'

          Generally I want to point on lpFunc a pointer to Write fuction but within class (I don't want to any globals). ----------- Mila -- modified at 3:15 Wednesday 28th June, 2006

          J Offline
          J Offline
          Justin Tay
          wrote on last edited by
          #4

          That's not how you declare a member function pointer.

          class CTest
          {
          public:
          CTest()
          {
          lpFunc = &CTest::Write;
          }
          long Write(DWORD dwInfo)
          {
          ...
          }
          long (CTest::*lpFunc)(DWORD); // declaration for member function pointer
          };

          1 Reply Last reply
          0
          • T toxcct

            Mila025 wrote:

            '=' : cannot convert from 'long (__thiscall CTest::* )(DWORD)' to 'long (__cdecl *)(DWORD)'

            notice the calling convention of the two parts of your operator = reported by the compiler. you function Write has to be either static or global. the fact is that being a class member function implicitely adds it the this pointer parameter...


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            T Offline
            T Offline
            talkshit1
            wrote on last edited by
            #5

            static need be there ONLY if it is not a member of the class.

            S 1 Reply Last reply
            0
            • T talkshit1

              static need be there ONLY if it is not a member of the class.

              S Offline
              S Offline
              super_ttd
              wrote on last edited by
              #6

              certainly not !!! compile the code and tell if the compiler complains... :suss: -- TTD --

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups