calling a static function
-
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]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:
-
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] -
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?
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:
-
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:
-
////////////////////////////
// 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:
-
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:
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
-
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]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]
-
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:
touché :laugh:
-
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]
Hey tox (aka Grammar Nazi)... Shouldn't you be busy working on this?[^] ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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:
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 aclass
)static
functions have file scope, whilestatic
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] -
Hey tox (aka Grammar Nazi)... Shouldn't you be busy working on this?[^] ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
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]
-
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 aclass
)static
functions have file scope, whilestatic
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]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]
-
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]
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:
-
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]
And you're right [^]. Anyway it looks like '
wish
' is needed in the sentence. Anyway I'm a poor Italian man... :-DIf 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] -
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 aclass
)static
functions have file scope, whilestatic
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]I stand corrected! :) And I'm not the one that voted you down! Cheers!
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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]
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] -
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]
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:
-
I stand corrected! :) And I'm not the one that voted you down! Cheers!
Mark Salsbery Microsoft MVP - Visual C++ :java:
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] -
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]CPallini wrote:
Kanine Crunchies
Now I'm hungry! :-D
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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]: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++]