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. about static function

about static function

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
16 Posts 4 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 Maer727

    - What is the difference between a normal function and a static function? - For example, static int StaticFunc(int n); - Can you show me an example? - Regards, Maer

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

    When static is applied to a global function it means the function is visible only in that .CPP file. --Mike-- http://home.inreach.com/mdunn/ #include "witty_sig.h" :love: your :bob: with :vegemite: and :beer:

    M 1 Reply Last reply
    0
    • M Maer727

      - What is the difference between a normal function and a static function? - For example, static int StaticFunc(int n); - Can you show me an example? - Regards, Maer

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

      I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of

      Bitmap bm;
      bm.FromFile("Claudia Schiffer.jpg");

      I do it like this:

      Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");

      You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

      A M 3 Replies Last reply
      0
      • C Christian Graus

        I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of

        Bitmap bm;
        bm.FromFile("Claudia Schiffer.jpg");

        I do it like this:

        Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");

        You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #4

        To answer your doubt about globally static functions. Basically this goes back to good old C, where there are only two types of functions, global and static. The syntactic difference is that static functions have the word "static" in their definition and global ones do not. Static functions are visible only inside the file in which they're compiled. Global functions are visible among all the files in the project. So if you have files A.c and B.c, they can each have a static function with the same name and the linker will treat them as two different functions. But if you remove the static modifier from them, the linker will see the same (global) function defined multiple times and give you an error. So, basically, in C you'd want to make a function "static" when it's only going to be used inside the file it's defined and you want to avoid possible name collisions. Regards, Alvaro

        C M 2 Replies Last reply
        0
        • A Alvaro Mendez

          To answer your doubt about globally static functions. Basically this goes back to good old C, where there are only two types of functions, global and static. The syntactic difference is that static functions have the word "static" in their definition and global ones do not. Static functions are visible only inside the file in which they're compiled. Global functions are visible among all the files in the project. So if you have files A.c and B.c, they can each have a static function with the same name and the linker will treat them as two different functions. But if you remove the static modifier from them, the linker will see the same (global) function defined multiple times and give you an error. So, basically, in C you'd want to make a function "static" when it's only going to be used inside the file it's defined and you want to avoid possible name collisions. Regards, Alvaro

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

          Cool - thanks for the reply, that expands on what Michael said and I understand how it works now. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

          1 Reply Last reply
          0
          • M Michael Dunn

            When static is applied to a global function it means the function is visible only in that .CPP file. --Mike-- http://home.inreach.com/mdunn/ #include "witty_sig.h" :love: your :bob: with :vegemite: and :beer:

            M Offline
            M Offline
            Maer727
            wrote on last edited by
            #6

            - Thanks pal! - Your reply helps a lot. But I still have a question. What means "global function"? - Can you show me an example? - Regards, Maer

            1 Reply Last reply
            0
            • C Christian Graus

              I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of

              Bitmap bm;
              bm.FromFile("Claudia Schiffer.jpg");

              I do it like this:

              Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");

              You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

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

              - Yep, Christian pal, I use the static function in a class and not used as a global function. - I still have a question. In your reply, you mentioned, "For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? - Have a nice day, Maer

              C 1 Reply Last reply
              0
              • M Maer727

                - Yep, Christian pal, I use the static function in a class and not used as a global function. - I still have a question. In your reply, you mentioned, "For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? - Have a nice day, Maer

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

                "For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? Probably no example better than the one I gave. In GDI+ the Bitmap class has a method called FromFile and it's static, which means I can start with a Bitmap *, which has not had new called on it yet, and then call the static method, which is a bit like a global function which returns a Bitmap * when given a filename, but being a static function it exists in the namespace of the class. So given that you'll only have one instance of the function AND the function does not require you to have created an instance of the class, a sensible use for them would be to call a function that creates an instance of the class and returns it. I'm sure there are other uses, I use a static function to do a reference count for example. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                M 1 Reply Last reply
                0
                • A Alvaro Mendez

                  To answer your doubt about globally static functions. Basically this goes back to good old C, where there are only two types of functions, global and static. The syntactic difference is that static functions have the word "static" in their definition and global ones do not. Static functions are visible only inside the file in which they're compiled. Global functions are visible among all the files in the project. So if you have files A.c and B.c, they can each have a static function with the same name and the linker will treat them as two different functions. But if you remove the static modifier from them, the linker will see the same (global) function defined multiple times and give you an error. So, basically, in C you'd want to make a function "static" when it's only going to be used inside the file it's defined and you want to avoid possible name collisions. Regards, Alvaro

                  M Offline
                  M Offline
                  Maer727
                  wrote on last edited by
                  #9

                  - Thanks, Alvaro pal! - Your reply helps a lot. I still have a question. I think the functionality of "static" changes in C++, it means a function that can be called without a pointer. - Since I am a newbie, I do not know whether I am correct. - Yep, still a trouble. Can the function of "static" in the oldie and goldie C still be used in C++? Are they the same? - Can you help? - Have a nice day, Maer

                  A 1 Reply Last reply
                  0
                  • C Christian Graus

                    "For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? Probably no example better than the one I gave. In GDI+ the Bitmap class has a method called FromFile and it's static, which means I can start with a Bitmap *, which has not had new called on it yet, and then call the static method, which is a bit like a global function which returns a Bitmap * when given a filename, but being a static function it exists in the namespace of the class. So given that you'll only have one instance of the function AND the function does not require you to have created an instance of the class, a sensible use for them would be to call a function that creates an instance of the class and returns it. I'm sure there are other uses, I use a static function to do a reference count for example. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                    M Offline
                    M Offline
                    Maer727
                    wrote on last edited by
                    #10

                    - Thanks, Christian pal! - Your reply clarifies my doubts. And I still have a stupid question. - For example, I use a static function named F1 in class A. Can I use the function F1 in another class B without creating an object (or pointer) of class A in class B. ( Suppose class B has no derivation relationship with class A) - Can you help? - Regards, Maer

                    C 1 Reply Last reply
                    0
                    • M Maer727

                      - Thanks, Christian pal! - Your reply clarifies my doubts. And I still have a stupid question. - For example, I use a static function named F1 in class A. Can I use the function F1 in another class B without creating an object (or pointer) of class A in class B. ( Suppose class B has no derivation relationship with class A) - Can you help? - Regards, Maer

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

                      In that case it becomes a question of the relationship between A and B. If the function is public or B is a friend of A then that would not be a problem. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                      M 1 Reply Last reply
                      0
                      • C Christian Graus

                        I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of

                        Bitmap bm;
                        bm.FromFile("Claudia Schiffer.jpg");

                        I do it like this:

                        Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");

                        You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                        M Offline
                        M Offline
                        Maer727
                        wrote on last edited by
                        #12

                        - Sorry for interrupting again pal! - I can not find the "FromFile" function. (MSDN-->CBitMap-->class menbers-->) - Can you help? - Regards, Maer

                        C 1 Reply Last reply
                        0
                        • C Christian Graus

                          In that case it becomes a question of the relationship between A and B. If the function is public or B is a friend of A then that would not be a problem. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                          M Offline
                          M Offline
                          Maer727
                          wrote on last edited by
                          #13

                          - Thanks, Christian pal! - You have clarified my doubts. - Regards, Maer

                          1 Reply Last reply
                          0
                          • M Maer727

                            - Thanks, Alvaro pal! - Your reply helps a lot. I still have a question. I think the functionality of "static" changes in C++, it means a function that can be called without a pointer. - Since I am a newbie, I do not know whether I am correct. - Yep, still a trouble. Can the function of "static" in the oldie and goldie C still be used in C++? Are they the same? - Can you help? - Have a nice day, Maer

                            A Offline
                            A Offline
                            Alvaro Mendez
                            wrote on last edited by
                            #14

                            - You're welcome. - In C++ "static" can also be used for class/struct member functions. This allows you to call it without having an instance of the class/struct. So if you have this:

                            class A
                            {
                            public: static void foo();
                            };

                            You can then call it directly, like this:

                            A::foo();

                            without needing to first create an instance of A. - Static C functions work the same way in C++. They're only local to the file in which they're compiled. So if you have a static C function, only that C file can see it. - Hope to have helped, but you really need to pick up a couple of C/C++ books and read them thoroughly. - Regards, Alvaro

                            M 1 Reply Last reply
                            0
                            • A Alvaro Mendez

                              - You're welcome. - In C++ "static" can also be used for class/struct member functions. This allows you to call it without having an instance of the class/struct. So if you have this:

                              class A
                              {
                              public: static void foo();
                              };

                              You can then call it directly, like this:

                              A::foo();

                              without needing to first create an instance of A. - Static C functions work the same way in C++. They're only local to the file in which they're compiled. So if you have a static C function, only that C file can see it. - Hope to have helped, but you really need to pick up a couple of C/C++ books and read them thoroughly. - Regards, Alvaro

                              M Offline
                              M Offline
                              Maer727
                              wrote on last edited by
                              #15

                              - Thanks pal! - You have clarified all my doubts. And thanks for your suggestions. - Regards, Maer

                              1 Reply Last reply
                              0
                              • M Maer727

                                - Sorry for interrupting again pal! - I can not find the "FromFile" function. (MSDN-->CBitMap-->class menbers-->) - Can you help? - Regards, Maer

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

                                It's a GDI+ function in the Bitmap class, not a GDI function in the CBItmap class. If you don't have the SDK installed, you won't have access to it, but it's covered in the MSDN. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                                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