Questions tagged [static-functions]

124 questions
169
votes
1 answer

Static member functions error; How to properly write the signature?

I am getting an error when trying to compile my code in g++ using the current signature: cannot declare member function static void Foo::Bar(std::ostream&, const Foo::Node*) to have static linkage My question is twofold: Why does it not Compile…
Joshua
  • 4,270
  • 10
  • 42
  • 62
161
votes
2 answers

Superiority of unnamed namespace over static?

How are unnamed namespaces superior to the static keyword?
Nawaz
  • 353,942
  • 115
  • 666
  • 851
83
votes
15 answers

Where would you use a friend function vs. a static member function?

We make a non-member function a friend of a class when we want it to access that class's private members. This gives it the same access rights as a static member function would have. Both alternatives would give you a function that is not associated…
Swapna
  • 889
  • 2
  • 8
  • 8
62
votes
2 answers

Does a static function need the static keyword for the prototype in C?

My C programming book says that when I want to create a static function, I need to put the static keyword in front of the function definition. It doesn't mention anything explicitly about the prototype. Also, the examples don't use prototypes and…
user2211907
29
votes
2 answers

Is it possible to specify a static function in a Kotlin interface?

I want to do something like this: interface Serializable { fun serialize(): ToType companion object { abstract fun deserialize(serialized: ToType): FromType } } or even this would work for me: interface…
Ky -
  • 30,724
  • 51
  • 192
  • 308
20
votes
2 answers

Static Function Help C++

I can't get past this issue I am having. Here's a simple example: class x { public: void function(void); private: static void function2(void); }; void x::function(void) { x::function2(void); } static void function2(void) { …
Alex
  • 426
  • 1
  • 6
  • 15
19
votes
4 answers

PHP constructors and static functions

I'm a bit confused on how constructors work in PHP. I have a class with a constructor which gets called when I instantiate a new object. $foo = new Foo($args); __construct($params) is called in the class Foo and it executes the appropriate…
bar1024
  • 231
  • 2
  • 4
  • 7
17
votes
2 answers

gcc warns about unused static functions, but not static inline: is there a practical distinction?

My version (5.4) of gcc warns about unused static functions, even in a header file when -Wall is used. It doesn't complain if the same functions are defined static inline or simply inline. For example, the following function in a file…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
17
votes
3 answers

Static functions in Linux device driver

Why is it that every function in most device drivers are static? As static functions are not visible outside of the file scope. Then, how do these driver function get called by user space applications?
16
votes
4 answers

How to obtain static member variables in MATLAB classes?

Is there a way to define static member variables in MATLAB classes? This doesn't work: classdef A properties ( Static ) m = 0; end end It suggests to use keyword "Constant" instead of "Static", the constant properties cannot be…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
12
votes
7 answers

Static Virtual functions in c++

I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static. How can I do that?
12
votes
2 answers

Are constexpr functions implicitly static?

If I define a function in my program.cpp: constexpr bool isThree(const int number) { return number == 3; } is that any different from declaring it static? static constexpr bool isThree(const int number) { return number == 3; } It seems that…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
10
votes
6 answers

static member function and thread-safety

In C++, when you have local variables in a static member function, does it mean those local variables are also implicitly static or are they really local? example: static void myClass::somefunc(int someint) { int myint = someint; // is myint…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
10
votes
3 answers

Static functions in Linux device driver?

Is there a reason why most function definition in device driver in linux code is defined as static? Is there a reason for this? I was told this is for scoping and to prevent namespace pollution, could anyone explain it in detail why static…
andycjw
  • 1,021
  • 1
  • 8
  • 11
10
votes
5 answers

C API function callbacks into C++ member function code

So, I'm using the FMOD api and it really is a C api. Not that that's bad or anything. Its just it doesn't interface well with C++ code. For example, using FMOD_Channel_SetCallback( channel, callbackFunc ) ; It wants a C-style function for…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
1
2 3
8 9