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. Static member funciton call non-static function

Static member funciton call non-static function

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 5 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.
  • Z Offline
    Z Offline
    Zero_G
    wrote on last edited by
    #1

    I have a static function and I want it to call a non-static function. The computer is telling me that I can not do this just by calling the non-static function outright. Is there a way to call the nonstatic function from the static function?

    B C 2 Replies Last reply
    0
    • Z Zero_G

      I have a static function and I want it to call a non-static function. The computer is telling me that I can not do this just by calling the non-static function outright. Is there a way to call the nonstatic function from the static function?

      B Offline
      B Offline
      bmzhao
      wrote on last edited by
      #2

      Pass a pointer as a parameter of static function, call the non-static function by pointer.

      1 Reply Last reply
      0
      • Z Zero_G

        I have a static function and I want it to call a non-static function. The computer is telling me that I can not do this just by calling the non-static function outright. Is there a way to call the nonstatic function from the static function?

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        If you need to, then the function should not be static. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

        N 1 Reply Last reply
        0
        • C Christian Graus

          If you need to, then the function should not be static. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

          N Offline
          N Offline
          nguyenvhn
          wrote on last edited by
          #4

          If it should not be static, so there are any problem. You should modify your function, that can accept an argument (this pointer). So that, in the function, you can call none static function from this pointer. For example:

          class A{
          static void Func(A* pA)
          {
          pA->NoneStaticFunc();
          }
          }

          C 1 Reply Last reply
          0
          • N nguyenvhn

            If it should not be static, so there are any problem. You should modify your function, that can accept an argument (this pointer). So that, in the function, you can call none static function from this pointer. For example:

            class A{
            static void Func(A* pA)
            {
            pA->NoneStaticFunc();
            }
            }

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            This is plain stupid. If you can only call a method when you have an instance of the class, the method should not be static. It in essence won't be static, it can only be called when you have an instance of the class to call it with, even if you don't call it from the class. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

            N 1 Reply Last reply
            0
            • C Christian Graus

              This is plain stupid. If you can only call a method when you have an instance of the class, the method should not be static. It in essence won't be static, it can only be called when you have an instance of the class to call it with, even if you don't call it from the class. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

              N Offline
              N Offline
              nguyenvhn
              wrote on last edited by
              #6

              No. I don't think so. According to C++ principle, static members should be shared with every instances of class. But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case.

              C 1 Reply Last reply
              0
              • N nguyenvhn

                No. I don't think so. According to C++ principle, static members should be shared with every instances of class. But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                nguyenvhn wrote: According to C++ principle, static members should be shared with every instances of class Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not. nguyenvhn wrote: But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case. We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static. If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                N S 2 Replies Last reply
                0
                • C Christian Graus

                  nguyenvhn wrote: According to C++ principle, static members should be shared with every instances of class Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not. nguyenvhn wrote: But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case. We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static. If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                  N Offline
                  N Offline
                  nguyenvhn
                  wrote on last edited by
                  #8

                  :)Oh oh, In fact, you are right. I have nothing to protest. Of course, a static member has class scope, so that it can call only others static members. But in this case, the guy need a solution to adapt with his problem (it may be by historic). Because he needed to call a none static member, it was evident that there had to be exist an instance of class. It is very clear that we shoud avoid such case but a trick in porogamming makes the life more funny;P and I like to solve that stuck:-D It is so interesting in reasoning with you.;P

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    nguyenvhn wrote: According to C++ principle, static members should be shared with every instances of class Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not. nguyenvhn wrote: But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case. We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static. If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

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

                    In general, I agree with Christian on this. Christian Graus wrote: A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. And Microsoft encourages this practice :) with Windows callback functions. The callback (if implemented as part of a C++ class) has to be a static member, but in general, what's done is that there's a programmer-supplied value (usually void*) that can be passed as well as the function address. In those cases the approach is to pass the address of an instance of the class, as has been previously suggested. Steve S Developer for hire

                    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