Discuss Static function pointer in the C Programming forum on Dev Shed. Static function pointer C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 6,252
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 13 m 3 sec
Reputation Power: 1985
I'm not completely sure, but it must be because of the nature of static class members.
In order to use (almost) any class member, you need to instantiate an object of that class, in which case the constructor should take care of initializing all pointer members. The exception to that rule is the static member, which does not depend on any object having been instantiated. In fact, you can still call and use a static class member even if absolutely no objects of that class exist. However, that also means that no constructor ever needs to have been called, which means that no pointers will have been initialized.
It therefore seems impossible to have a static pointer member since it could never be initialized.
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,402
Time spent in forums: 2 Months 8 h 53 m 6 sec
Reputation Power: 4080
For static function pointers (or for that matter, any kind of static member variable), you need to initialize them outside a method, like you're initializing a global variable:
CFPointerTest.h
Code:
#ifndef CFPOINTER_TEST
#define CFPOINTER_TEST
class CFPointerTest{
private:
public:
CFPointerTest();
static int (*test)();
static void setF();
static int f1();
};
#endif
You need to initialize like this because a static member variable is common to all instances of a class and should be initialized always before any instances of class are instantiated.
__________________ Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Last edited by Scorpions4ever : September 8th, 2012 at 02:13 PM.
Posts: 13
Time spent in forums: 5 h 3 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by dwise1_aol
....However, that also means that no constructor ever needs to have been called, which means that no pointers will have been initialized.
It therefore seems impossible to have a static pointer member since it could never be initialized.
Sorry, I dont really understand. Im no expert. How do you mean initialized? It just holds a memory adress like an int holds a number. I can assign something to a static int member(through a static function) without the constructor being called. Why is it different?
Thanks!
EDIT:
Sorry! I remember now, ...like with singletons.
(like CKeys* CKeys:Instance= 0; in the cpp)
Thanks!
Posts: 4,824
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
Originally Posted by Aliii
How do you mean initialized?
The problem is not in fact one of initialisation, but rather one of instantiation. You have to create the static instance because it is independent of any single object instance.
Posts: 1,938
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Quote:
Originally Posted by Aliii
Sorry, I dont really understand. Im no expert. How do you mean initialized? It just holds a memory adress like an int holds a number. I can assign something to a static int member(through a static function) without the constructor being called. Why is it different?
Thanks!
It's for exactly the same reason as when you have a header file with
Code:
extern int x;
you need a source file (or "compilation unit") that defines x. The header file only declares that x exists; it doesn't set aside any space for it. A static member of a class is just like that: the "static ..." declaration in the class body only declares that this member exists; it doesn't reserve any space for it (unlike a non-static member, which has space reserved for it in each instance of the class). When you define the static member in a source file and compile that file, the resulting object file will have the necessary bytes reserved for it in its data section.
Posts: 13
Time spent in forums: 5 h 3 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by Scorpions4ever
For static function pointers (or for that matter, any kind of static member variable), you need to initialize them outside a method, like you're initializing a global variable:
CFPointerTest.h
Code:
#ifndef CFPOINTER_TEST
#define CFPOINTER_TEST
class CFPointerTest{
private:
public:
CFPointerTest();
static int (*test)();
static void setF();
static int f1();
};
#endif
You need to initialize like this because a static member variable is common to all instances of a class and should be initialized always before any instances of class are instantiated.
Thanks! It works now.
I just dont understand that syntax.
Why this:
int (*CFPointerTest::test)() = f1;
And why not this?
int CFPointerTest:(*test)())= f1;