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. C++ calling member functions statically

C++ calling member functions statically

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++ooptutorial
3 Posts 2 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
    Matthew Bache
    wrote on last edited by
    #1

    Naive programmer question time... I'm reviewing some old code (not mine) for my employer and have found that some member functions are being called statically. Is this good practice? Is there any benefit to calling member functions statically? Example code structure as below. class MyClass { //... public: void fn2() {//...} void fn1() {MyClass::fn2();} // why not just fn2() ? }; Is there a reason/benefit why fn2() gets called statically? Both fn1 and fn2 alter the calling object. Neither fn affects any static class data. MyClass is not invloved in any inheritance.

    Matt

    S 1 Reply Last reply
    0
    • M Matthew Bache

      Naive programmer question time... I'm reviewing some old code (not mine) for my employer and have found that some member functions are being called statically. Is this good practice? Is there any benefit to calling member functions statically? Example code structure as below. class MyClass { //... public: void fn2() {//...} void fn1() {MyClass::fn2();} // why not just fn2() ? }; Is there a reason/benefit why fn2() gets called statically? Both fn1 and fn2 alter the calling object. Neither fn affects any static class data. MyClass is not invloved in any inheritance.

      Matt

      S Offline
      S Offline
      Steve S
      wrote on last edited by
      #2

      It isn't a static member function, so it isn't being called statically. Instead, it is being called explicitly. If it were a virtual function, then you would be guaranteeing to call the given class implementation (or the one it inherits, but there isn't any inheritance in this case. There's no real advantage/disadvantage in this case, except that the compiler is being explicity told to use MyClass::fn2() rather than this->fn2().

      Steve S Developer for hire

      M 1 Reply Last reply
      0
      • S Steve S

        It isn't a static member function, so it isn't being called statically. Instead, it is being called explicitly. If it were a virtual function, then you would be guaranteeing to call the given class implementation (or the one it inherits, but there isn't any inheritance in this case. There's no real advantage/disadvantage in this case, except that the compiler is being explicity told to use MyClass::fn2() rather than this->fn2().

        Steve S Developer for hire

        M Offline
        M Offline
        Matthew Bache
        wrote on last edited by
        #3

        Thanks for the explanation.

        Matt

        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