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. calling a static function

calling a static function

Scheduled Pinned Locked Moved C / C++ / MFC
help
32 Posts 7 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.
  • S steph5

    Any ideas how I can have a function I can call from anywhere but where I don't need an instance of the class to call it?

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #15

    You can call a static method of a class from other cpp files! You need to link the files however. The problem is in your project - the linker isn't finding the file with the function being called. Fix that and it will work. The two files in your example ARE in the same project, right?

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    S 1 Reply Last reply
    0
    • M Mark Salsbery

      CPallini wrote:

      That's true.

      Huh? I call static methods that aren't in the same source file all the time :)

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S Offline
      S Offline
      steph5
      wrote on last edited by
      #16

      how?

      M 1 Reply Last reply
      0
      • S steph5

        how?

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #17

        ////////////////////////////
        // myClass.h
        ////////////////////////////

        class myClass
        {
        public:
        static void StaticMethod();
        };

        ////////////////////////////
        // myClass.cpp
        ////////////////////////////

        void myClass::StaticMethod()
        {
        }

        ////////////////////////////
        // someother.cpp
        ////////////////////////////

        void somefunc()
        {
        myClass::StaticMethod();
        }

        Maybe you forgot the part in red above...

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • M Mark Salsbery

          You can call a static method of a class from other cpp files! You need to link the files however. The problem is in your project - the linker isn't finding the file with the function being called. Fix that and it will work. The two files in your example ARE in the same project, right?

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S Offline
          S Offline
          steph5
          wrote on last edited by
          #18

          yes all the files are incluceded in the same project. how do I make sure the linker finds the file? The thing is I can call myClass::myFunction() from one of the .cpp files in the project, but I can't call it from another .cpp file in the same project. whats going on? Thanks for your time

          1 Reply Last reply
          0
          • C CPallini

            Nope. ;)

            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.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

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

            you're saying this as if such construction never exists... but I'm sure it is ! for example, the following is perfectly valid : "If I were you, I wouldn't do that"... ;P

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            M C 2 Replies Last reply
            0
            • M Mark Salsbery

              toxcct wrote:

              not a native english speaker (like you)

              LOL! I'm pretty sure the rest of the world doesn't call what we Americans speak "English" ;P

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              J Offline
              J Offline
              john john mackey
              wrote on last edited by
              #20

              touché :laugh:

              1 Reply Last reply
              0
              • T toxcct

                you're saying this as if such construction never exists... but I'm sure it is ! for example, the following is perfectly valid : "If I were you, I wouldn't do that"... ;P

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #21

                Hey tox (aka Grammar Nazi)... Shouldn't you be busy working on this?[^] ;P

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                T 1 Reply Last reply
                0
                • M Mark Salsbery

                  CPallini wrote:

                  That's true.

                  Huh? I call static methods that aren't in the same source file all the time :)

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

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

                  Actually his sentence,

                  steph5 wrote:

                  I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                  and yours

                  Mark Salsbery wrote:

                  Huh? I call static methods that aren't in the same source file all the time

                  are not in conflict: standard (i.e C-like, not belonging to a class) static functions have file scope, while static methods have not such a constraint. :)

                  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.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  T M 2 Replies Last reply
                  0
                  • M Mark Salsbery

                    Hey tox (aka Grammar Nazi)... Shouldn't you be busy working on this?[^] ;P

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

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

                    Mark, wait. I didn't say all I write is gramatically correct; I make mistakes sometimes. BUT, isn't the construction I just asked you in this thread valid ?

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    M 2 Replies Last reply
                    0
                    • C CPallini

                      Actually his sentence,

                      steph5 wrote:

                      I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                      and yours

                      Mark Salsbery wrote:

                      Huh? I call static methods that aren't in the same source file all the time

                      are not in conflict: standard (i.e C-like, not belonging to a class) static functions have file scope, while static methods have not such a constraint. :)

                      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.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

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

                      CPallini wrote:

                      while static methods

                      C++ (actually Bjarne Stroustrup) don't talk about methods (which is more correct to Java/C#), but talks about member variables and member functions.

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      C 1 Reply Last reply
                      0
                      • T toxcct

                        Mark, wait. I didn't say all I write is gramatically correct; I make mistakes sometimes. BUT, isn't the construction I just asked you in this thread valid ?

                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #25

                        I'm just goofin with you here, of course.

                        toxcct wrote:

                        isn't the construction I just asked you in this thread valid ?

                        Where at? I missed something somewhere :)

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        1 Reply Last reply
                        0
                        • T toxcct

                          you're saying this as if such construction never exists... but I'm sure it is ! for example, the following is perfectly valid : "If I were you, I wouldn't do that"... ;P

                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                          And you're right [^]. Anyway it looks like 'wish' is needed in the sentence. Anyway I'm a poor Italian man... :-D

                          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.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          1 Reply Last reply
                          0
                          • C CPallini

                            Actually his sentence,

                            steph5 wrote:

                            I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                            and yours

                            Mark Salsbery wrote:

                            Huh? I call static methods that aren't in the same source file all the time

                            are not in conflict: standard (i.e C-like, not belonging to a class) static functions have file scope, while static methods have not such a constraint. :)

                            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.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #27

                            I stand corrected! :) And I'm not the one that voted you down! Cheers!

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            C 1 Reply Last reply
                            0
                            • T toxcct

                              CPallini wrote:

                              while static methods

                              C++ (actually Bjarne Stroustrup) don't talk about methods (which is more correct to Java/C#), but talks about member variables and member functions.

                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                              OOP talks about methods. C++ use its own jargon to name the same entity. :)

                              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.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              1 Reply Last reply
                              0
                              • T toxcct

                                Mark, wait. I didn't say all I write is gramatically correct; I make mistakes sometimes. BUT, isn't the construction I just asked you in this thread valid ?

                                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #29

                                I see it now sorry :) Yeah you are right I think. The more I say both versions, yours sounds better. And using "was" implies past tense which seems silly. :beer:

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  I stand corrected! :) And I'm not the one that voted you down! Cheers!

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

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

                                  Mark Salsbery wrote:

                                  I stand corrected!

                                  did you ever doubt if? ;P

                                  Mark Salsbery wrote:

                                  And I'm not the one that voted you down!

                                  No doubt about. BTW: no Kanine Crunchies for my troll this evening. :)

                                  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.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

                                  M 1 Reply Last reply
                                  0
                                  • C CPallini

                                    Mark Salsbery wrote:

                                    I stand corrected!

                                    did you ever doubt if? ;P

                                    Mark Salsbery wrote:

                                    And I'm not the one that voted you down!

                                    No doubt about. BTW: no Kanine Crunchies for my troll this evening. :)

                                    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.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    M Offline
                                    M Offline
                                    Mark Salsbery
                                    wrote on last edited by
                                    #31

                                    CPallini wrote:

                                    Kanine Crunchies

                                    Now I'm hungry! :-D

                                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                                    1 Reply Last reply
                                    0
                                    • C CPallini

                                      steph5 wrote:

                                      I was in the understanding that if you declare a static function it can only be called from the source file where it is declared.

                                      That's true.

                                      steph5 wrote:

                                      Am I missing something??

                                      Yes: in

                                      steph5 wrote:

                                      class myClass { static void myFunction }

                                      is not an ordinary (i.e. C-style) static function, is a static method (i.e is a member of the class even if you don't need an instance of the class to call it). :)

                                      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.
                                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                      [My articles]

                                      R Offline
                                      R Offline
                                      Rajesh R Subramanian
                                      wrote on last edited by
                                      #32

                                      :suss: Why the low vote? Someone had a bad day. Perhaps. here[^] as well.

                                      Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                                      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