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 function

static function

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 4 Posters 4 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
    Maynka
    wrote on last edited by
    #1

    Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards

    E S C 4 Replies Last reply
    0
    • M Maynka

      Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards

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

      create the class as a member varible of the class to which you call the functions ClassA has m_classB ClassB has the functions your after.

      1 Reply Last reply
      0
      • M Maynka

        Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards

        E Offline
        E Offline
        Emmanouil
        wrote on last edited by
        #3

        Yes you can use static functions for that. You can have like a utility class that you can put helper functions in there. Example usage will be like the following code: //======================== class A{ public: static void myprint(){ printf("static hello ! \n"); } }; class B{ public: void func(){ A::myprint(); // Here you call the static myprint without having to create an object of class A } }; int main() { B *poB = new B(); poB->func(); A::myprint() // Again, you can call myprint with no object creation } //======================== The output of this will be : static hello! static hello! Hope that helps :-)

        S M 2 Replies Last reply
        0
        • M Maynka

          Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards

          E Offline
          E Offline
          Emmanouil
          wrote on last edited by
          #4

          However you have to take care of static functions and use these only if you are sure that they fit to your design. An alternative way to do this is to just derive the classes that needs your functions from the class they belong to. i.e class A{ public: void funcA(); void funcB(); }; class B: public A { // inherits class A // this class now can use the funcA and funcB as its own }; Its basic OO C++ :-)

          1 Reply Last reply
          0
          • M Maynka

            Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Maynka wrote:

            I don;t want to create an object of the class to which the function belongs everywhere.

            You don't need to create objects everywhere, you can accomplish the task with a single instance of the class (the better way to do this is explained by the singleton design pattern).

            Maynka wrote:

            If making a function static will help:

            Probably this is the most natural solution, because it seems that you don't need to access instance members in your functions (and, indeed, you cannot access such members in static functions). :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            1 Reply Last reply
            0
            • E Emmanouil

              Yes you can use static functions for that. You can have like a utility class that you can put helper functions in there. Example usage will be like the following code: //======================== class A{ public: static void myprint(){ printf("static hello ! \n"); } }; class B{ public: void func(){ A::myprint(); // Here you call the static myprint without having to create an object of class A } }; int main() { B *poB = new B(); poB->func(); A::myprint() // Again, you can call myprint with no object creation } //======================== The output of this will be : static hello! static hello! Hope that helps :-)

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

              :) nice didn't know that or atleast i forgot about it more likely i missed it in a lecture due to me :zzz: knew that static keeps the varibles and function on the heep (am i correct?) didnt realise it affected how it could be called? is that good programming standard or not? thanks sam

              E 1 Reply Last reply
              0
              • E Emmanouil

                Yes you can use static functions for that. You can have like a utility class that you can put helper functions in there. Example usage will be like the following code: //======================== class A{ public: static void myprint(){ printf("static hello ! \n"); } }; class B{ public: void func(){ A::myprint(); // Here you call the static myprint without having to create an object of class A } }; int main() { B *poB = new B(); poB->func(); A::myprint() // Again, you can call myprint with no object creation } //======================== The output of this will be : static hello! static hello! Hope that helps :-)

                M Offline
                M Offline
                Maynka
                wrote on last edited by
                #7

                Thanks i was needing this answer. Regards, Mayank

                1 Reply Last reply
                0
                • S Sam_c

                  :) nice didn't know that or atleast i forgot about it more likely i missed it in a lecture due to me :zzz: knew that static keeps the varibles and function on the heep (am i correct?) didnt realise it affected how it could be called? is that good programming standard or not? thanks sam

                  E Offline
                  E Offline
                  Emmanouil
                  wrote on last edited by
                  #8

                  mmm not exactly. If I remember correctly they are allocated in their own memory space. Cannot remember the name though.. have to look it up. Regarding whether or not is a good programming standard or not...well...it depends on how to use them. They should be avoided if another way is possible and good OO techniques should be used instead. Sometimes they are usefull though and their use might be required. Of course I wouldn't use static functions all over the place because static functions have their limitations and so they should be used with care.

                  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