Solved

pointer to a static function

Posted on 2003-11-12
7
226 Views
Last Modified: 2010-04-01
To C++ experts,

If I have a class, and a function like :
class X{} ;
static X* fun(int*) ;

How do I declare a pointer to a function which can point to fun ?
thanks.

meow.
0
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 20 total points
ID: 9737786
Use of the word static for a function to prevent it from having public linkage is now deprecated (though I must confess I do it all the time). You should really use an unnamed namespace instead, if you want to prevent the function from having public linkage.

--------8<--------
#include <iostream>

class X {};

namespace {
        class X x;
        X* fun(int*)
        {
                return &x;
        }
}

int main()
{
        // Get a pointer to the function
        X* (*ptr)(int*) = fun;

        // Use it
        int myint = 123;
        X* xp = (*ptr)(&myint);
}
--------8<--------
0
 
LVL 3

Expert Comment

by:freewell
ID: 9737793
int nData = 10;
X* (*pMyFunction)(int*);

pMyFunction = fun;
pMyFunction(&nData);
0
 
LVL 5

Expert Comment

by:migoEX
ID: 9737799
Exactly if there was no 'static' keyword - this only means the function is accessible only from this file. Even when you declare static function inside the class, the syntax would be the same.

X* (* tFunnc)(int*);

tFunc myFunc = fun;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:meow00
ID: 9741140
Hi,

    Could anyone please tell me that why not use
 "(static X*)(*pMyFunction)(int*);" ? why I don't need static in the very beginning ? Thanks !

meow.
0
 
LVL 5

Assisted Solution

by:migoEX
migoEX earned 20 total points
ID: 9741331
'static' is a definition of a SCOPE of a function, not a TYPE, which is parameters and return value.

Exactly as:

class A{
   public:
   static int s_i;
};

the type of s_i is 'int' and not 'static int'
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9745451
Try the following code snippet

int f () {
}
static int f () {
}


This would give u a compile error saying "redefinition of `int f()' "

This demonstrates that static is not at all related to the function signature

Now, technically, static is a type of storage class, thus it only decides what storage class the function belongs to and the behaviour of the compiler gets affected in the corresponding way

When u are declaring a pointer to a function, u just need the function signature and not the storage class of the function

That's the reason for your code to work

HTH

Amit
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249111
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: rstaveley {http:#9737786} & migoEX {http:#9741331}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question