473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pointer to function problem

#include <cstdlib>
#include <iostream>

using namespace std;

int (*test)();
int (*test2)();

int aku(){

static int i = 0;
cout << "NO : " << i << endl;
i++;
}
int main()
{

test = &aku;
test2 = &aku;
(*test)();
(*test)();

return 0;
}
The output is :
NO : 0
NO : 1

My question is,how to make test and test2 independent,although they
refer to same function.
That's mean the output is :
NO : 0
NO : 0

Mar 19 '06 #1
3 1496
bl*********@gmail.com wrote:
#include <cstdlib>
#include <iostream>

using namespace std;

int (*test)();
int (*test2)();

int aku(){

static int i = 0;
cout << "NO : " << i << endl;
i++;
}
int main()
{

test = &aku;
test2 = &aku;
(*test)();
(*test)();

return 0;
}
The output is :
NO : 0
NO : 1

My question is,how to make test and test2 independent,although they
refer to same function.
That's mean the output is :
NO : 0
NO : 0


You cannot. Unless you create a new function (this includes
different template instanciations), the static variable inside that
function will have those 'static' semantics you are experiencing.

hth
--
jb

(reply address in rot13, unscramble first)
Mar 19 '06 #2
posted:
#include <cstdlib>
#include <iostream>

using namespace std;

int (*test)();
int (*test2)();

int aku(){

static int i = 0;
cout << "NO : " << i << endl;
i++;
}
int main()
{

test = &aku;
test2 = &aku;
(*test)();
(*test)();

return 0;
}
The output is :
NO : 0
NO : 1

My question is,how to make test and test2 independent,although they
refer to same function.
That's mean the output is :
NO : 0
NO : 0


One way comes to mind:
#include <cstdlib>
#include <iostream>

using namespace std;

int (*test)();
extern char const funcpoint_name_test[] = "test";

int (*test2)();
extern char const funcpoint_name_test2[] = "test2";
template<const char* name_of_pointer>
int aku()
{

static int i = 0;
cout << "NO : " << i << endl;
++i;
}

int main()
{

test = &aku<funcpoint_name_test>;

test2 = &aku<funcpoint_name_test2>;

(*test)();

(*test2)();

return 0;
}
Mar 19 '06 #3
In article <1142771353.247507.231190
@v46g2000cwv.googlegroups.com>, bl*********@gmail.com
says...

[ ... ]
My question is,how to make test and test2 independent,although they
refer to same function.
That's mean the output is :
NO : 0
NO : 0


Using pointers to a function, you can't. You can,
however, create a functor that does the job:

struct aku {
int i;
aku() : i(0) {}

void operator()() {
std::cout << "NO: " << i << std::endl;
++i;
}
};

Each object you create of this type has its own,
independent counter. For example:

int main() {
aku test1;
aku test2;

test1();
test2();
test1();
test2();
return 0;
}

produces:

NO: 0
NO: 0
NO: 1
NO: 1

as its output. I think that's what you wanted anyway.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.