views:

2615

answers:

7

Ok, I understand what is static variable is, but what is a "static" function?

And why is it that if I declare function let's say "void print_matrix" in let's say a.cpp (WITHOUT a.hpp) and include "a.cpp" - I get "print_matrix@@....) already defined in a.obj", BUT if I declare it "static void print_matrix" then it compiles?

UPDATE Just to clear things up - I know that including ".cpp" is bad, as many of you pointed out, I just do it to temporarily clear space in main.cpp until I have better idea of how to group all those functions into proper .hpp and .cpp. Just a temporary quick solution.

+11  A: 

static functions are functions that are only visible to other functions in the same file.

EDIT: For those who thought, that the author of the questions meant a 'class method': No he means a plain old C function. For class methods, static obviously means that this method can be called on the class itself, no instance of that class necessary.

Johannes Weiß
This is only partially true. The original question was tagged C++, not C.
Andrew Grant
Actually I didn't tag it c++, some of admins probably did, but it was about C++, so what's the difference in C++?
Slava N
Parrots answer is correct with respect to C++ and classes. This is the correct answer for non-class functions.
Andrew Grant
Well, I pretty much asked about "function", not "method", so this one is what I asked.
Slava N
C++ methods are often referred to as "member functions", so I agree that C++ introduces a little bit of ambiguity. It's not your fault — the language just uses the keyword for two different things.
Chuck
yes. And a blatant abuse of the word static on the part of the language designers.
Assaf Lavie
I've never used the word "method" in C++.
Daniel Daranas
Thats just fine, but don't you agree that it way clear Slava meant a normal function?
Johannes Weiß
@Johannes Yes, because otherwise the question body doesn't make any sense at all. However, the question title makes many people think about the most common use of "static" in functions in C++, which is in member functions (because member functions are far more common than nonmember ones).
Daniel Daranas
Daniel, yes. thats true. When I 'opened' the question I was thinking I'll explain class-static. Then I read the question and realized that I have to explain normal-function-static ;-). But remember: The question has be retagged by someone else to C++.
Johannes Weiß
+3  A: 

A static function is one that can be called on the class itself, as opposed to an instance of the class.

For example a non-static would be:

Person* tom = new Person();
tom->setName("Tom");

This method works on an instance of the class, not the class itself. However you can have a static method that can work without having an instance. This is sometimes used in the Factory pattern:

Person* tom = Person::createNewPerson();
Parrots
It seems to me that you are talking about static "method", not "function"??
Slava N
I assumed you were referring to static functions within a class.
Parrots
If I have known "methods" are called "method functions" in C++, I'd be more clear on that. Well, now I do :) Thanks anyway
Slava N
There are no "methods" in C++, just functions. The C++ standard doesn't ever mention "methods", just "functions".
Brian Neal
+6  A: 

static function definitions will mark this symbol as internal. So it will not be visible for linking from outside, but only to functions in the same compilation unit, usually the same file.

Raim
+3  A: 

[Significantly edited]

First: It's generally a bad idea to include a .cpp file in another file - it leads to problems like this :-) The normal way is to create separate compilation units, and add a header file for the included file.

Secondly:

C++ has some confusing terminology here - I didn't know about it until pointed out in comments.

a) static functions - inherited from C, and what you are talking about here. Outside any class. A static function means that it isn't visible outside the current compilation unit - so in your case a.obj has a copy and your other code has an independent copy. (Bloating the final executable with multiple copies of the code).

b) static member function - what OO terms a static method. Lives inside a class. You call this with the class rather than through an object instance.

These two different static function definitions are completed different. Be careful - here be dragons.

Douglas Leeder
Well, I do it just to clear up some space TEMPORARILY in main.cpp until I decide how to organize the file into libraries along with proper .hpp's. Is there a better idea how to do this?
Slava N
The correct terminology in C++ is member function, not method. There are no "methods" in C++ legalese. Method is a general OO term. C++ implements them via member functions.
Brian Neal
@Brian - you're right - I'll correct it.
Douglas Leeder
+20  A: 

There is a big difference between static functions in C and static member functions in C++. In C, a static function is not visible outside of its translation unit, which is the object file it is compiled into. In other words, making a function static limits its scope. You can think of a static function as being "private" to its *.c file (although that is not strictly correct).

In C++, "static" applies to member functions and data members of classes. A static data member is also called a "class variable", while a non-static data member is an "instance variable". This is Smalltalk terminology. This means that there is only one copy of a static data member shared by all objects of a class, while each object has its own copy of a non-static data member. So a static data member is essentially a global variable, that is a member of a class.

Non-static member functions can access all data members of the class: static and non-static. Static member functions can only operate on the static data members.

One way to think about this is that in C++ static data members and static member functions do not belong to any object, but to the entire class.

Dima
+10  A: 

There are two uses for the keyword static when it comes to functions in C++.

The first is to mark the function as having internal linkage so it cannot be referenced in other translation units. This usage is deprecated in C++. Unnamed namespaces are preferred for this usage.

// inside some .cpp file:

static void foo();    // old "C" way of having internal linkage

// C++ way:
namespace
{
   void this_function_has_internal_linkage()
   {
      // ...
   }
}

The second usage is in the context of a class. If a class has a static member function, that means the function is a member of the class (and has the usual access to other members), but it doesn't need to be invoked through a particular object. In other words, inside that function, there is no "this" pointer.

Brian Neal
+2  A: 

Minor nit: static functions are visible to a translation unit, which for most practical cases is the file the function is defined in. The error you are getting is commonly referred to as violation of the One Definition Rule.

The standard probably says something like:

"Every program shall contain exactly one definition of every noninline function or object that is used in that program; no diagnostic required."

That is the C way of looking at static functions. This is deprecated in C++ however.

In C++, additionally, you can declare member functions static. These are mostly metafunctions i.e. they do not describe/modify a particular object's behavior/state but act on the whole class itself. Also, this means that you do not need to create an object to call a static member function. Further, this also means, you only get access to static member variables from within such a function.

I'd add to Parrot's example the Singleton pattern which is based on this sort of a static member function to get/use a single object throughout the lifetime of a program.

dirkgently