C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 8th, 2012, 01:14 PM
Aliii Aliii is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 13 Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 h 3 m 59 sec
Reputation Power: 0
Static function pointer

Why cant I do the following:

..........................header file:
class CFPointerTest{

private:

public:
CFPointerTest();

static int (*test)();

static void setF();
static int f1();

};

..........................cpp file:
CFPointerTest::CFPointerTest(){



}

void CFPointerTest::setF(){

CFPointerTest::test= f1;

}

int CFPointerTest::f1(){

return 1;

}

........................................
It says "undefined reference to `CFPointerTest::test'"
It works when test and setF are not static.

Reply With Quote
  #2  
Old September 8th, 2012, 01:32 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #3  
Old September 8th, 2012, 02:10 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,402 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
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

CFPointerTest.cpp
Code:
#include "CFPointerTest.h"

CFPointerTest::CFPointerTest() {
}

int (*CFPointerTest::test)() = f1;

void CFPointerTest::setF() {
    //CPointerTest::test = f1;
}

int CFPointerTest::f1() {
    return 1;
}


That will make it work.

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.

Reply With Quote
  #4  
Old September 8th, 2012, 02:16 PM
Aliii Aliii is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 13 Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level) 
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!

Reply With Quote
  #5  
Old September 8th, 2012, 02:28 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #6  
Old September 8th, 2012, 02:39 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #7  
Old September 8th, 2012, 03:48 PM
Aliii Aliii is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 13 Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level) 
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

CFPointerTest.cpp
Code:
#include "CFPointerTest.h"

CFPointerTest::CFPointerTest() {
}

int (*CFPointerTest::test)() = f1;

void CFPointerTest::setF() {
    //CPointerTest::test = f1;
}

int CFPointerTest::f1() {
    return 1;
}


That will make it work.

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;
Comments on this post
dwise1_aol disagrees: Disable smilies! They serve no purpose and they almost always completely frak up C++ code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Static function pointer

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap