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. Managed C++/CLI
  4. printing the address of a non static member function of a clss

printing the address of a non static member function of a clss

Scheduled Pinned Locked Moved Managed C++/CLI
6 Posts 3 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.
  • A Offline
    A Offline
    Amr Shahin
    wrote on last edited by
    #1

    hi everyone im trying to check if my compiler creates a copy of the nonstatic member functions for each object or it just shares the code. to do that i wrote this code: /******************************* #include using namespace std; class cls { public: int f( ) { //cout << (void*)(f) <

    T M 2 Replies Last reply
    0
    • A Amr Shahin

      hi everyone im trying to check if my compiler creates a copy of the nonstatic member functions for each object or it just shares the code. to do that i wrote this code: /******************************* #include using namespace std; class cls { public: int f( ) { //cout << (void*)(f) <

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

      firstly, you can be sure that the compiler shares only one copy of the member functions body. but you could do this to test :

      class cls {
      public;
      void f() {
      }
      };

      void main() {
      cls a, b;
      printf("%l %l", (void*)(&(a.f)), (void*)(&(b.f)));
      }

      -- modified at 13:19 Monday 6th March, 2006 (thanks Mike)

      A 1 Reply Last reply
      0
      • T toxcct

        firstly, you can be sure that the compiler shares only one copy of the member functions body. but you could do this to test :

        class cls {
        public;
        void f() {
        }
        };

        void main() {
        cls a, b;
        printf("%l %l", (void*)(&(a.f)), (void*)(&(b.f)));
        }

        -- modified at 13:19 Monday 6th March, 2006 (thanks Mike)

        A Offline
        A Offline
        Amr Shahin
        wrote on last edited by
        #3

        the code u gave me didnt work!! another thing if the compiler always shares the space why would static functions exist in the first place???!!!

        T 1 Reply Last reply
        0
        • A Amr Shahin

          the code u gave me didnt work!! another thing if the compiler always shares the space why would static functions exist in the first place???!!!

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

          Amr Shahin wrote:

          the code u gave me didnt work!!

          why ?? any error ?

          Amr Shahin wrote:

          if the compiler always shares the space why would static functions exist in the first place???!!!

          the difference between static/non-static member function are for the implicit this parameter. generally, a member function knows on which object it works on, because it is getting an implicit parameter, which is in C++ the this pointer. i say generally because static member function don't ! they can only perform general task, and can only access static members... but no way this has to relates with body copies. a function body is written once in the source code, do why would it be loaded multiple times in memory. the only difference between the calls are the parameters the function receives, but this is a stack problem...

          1 Reply Last reply
          0
          • A Amr Shahin

            hi everyone im trying to check if my compiler creates a copy of the nonstatic member functions for each object or it just shares the code. to do that i wrote this code: /******************************* #include using namespace std; class cls { public: int f( ) { //cout << (void*)(f) <

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            Amr Shahin wrote:

            cout << (void*)f <<endl;

            Change that to:

            cout << (void*) &cls::f <<endl;

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            A 1 Reply Last reply
            0
            • M Michael Dunn

              Amr Shahin wrote:

              cout << (void*)f <<endl;

              Change that to:

              cout << (void*) &cls::f <<endl;

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              A Offline
              A Offline
              Amr Shahin
              wrote on last edited by
              #6

              i tried ur code and it gave me the following error: /*********************** test.cpp:13: error: invalid use of non-static member function ‘void cls::f()’ test.cpp:13: error: converting from ‘void (cls::)()’ to ‘void ****************************!!!/ any ideas ??

              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