pointers to functions
-
How do you use a pointer to a function that is declared in the general scope within a class. Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?
void somefunction(int i);
void (*pointertosomef)(int i) = somefunction;class SomeObject
{
void (*m_pointertosomef)(int i);
public:
void ImportPointerToF(void (*pointertosomef)(int i));
void UsePointerToF();
}void SomeObject::ImportPointerToF(void (*pointertosomef)(int i))
{
m_pointertosomef = pointertosomef;
}
void SomeObject::UsePointerToF()
{
m_pointertosomef(30);
}I found on the internet the basic version of declaring a pointer function
void fun(int a);
void (*fun_ptr)(int) = fun;
fun\_ptr(10);
Everything else is my speculation
Yes, that works.
Mircea
-
How do you use a pointer to a function that is declared in the general scope within a class. Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?
void somefunction(int i);
void (*pointertosomef)(int i) = somefunction;class SomeObject
{
void (*m_pointertosomef)(int i);
public:
void ImportPointerToF(void (*pointertosomef)(int i));
void UsePointerToF();
}void SomeObject::ImportPointerToF(void (*pointertosomef)(int i))
{
m_pointertosomef = pointertosomef;
}
void SomeObject::UsePointerToF()
{
m_pointertosomef(30);
}I found on the internet the basic version of declaring a pointer function
void fun(int a);
void (*fun_ptr)(int) = fun;
fun\_ptr(10);
Everything else is my speculation
-
Does it compile, build and run without errors? The only caveat I would mention is that pointers to class methods require the method to be static.
ok, thanks to both of you.
Quote:
Does it compile, build and run without errors
Usually I don`t try to compile wild guesses. I did things as you would do with a pointer in my post above but often in c++ things that resemble in some places don`t have a syntax that matches everywhere.
-
ok, thanks to both of you.
Quote:
Does it compile, build and run without errors
Usually I don`t try to compile wild guesses. I did things as you would do with a pointer in my post above but often in c++ things that resemble in some places don`t have a syntax that matches everywhere.
Calin Cali wrote:
Usually I don`t try to compile wild guesses.
Well, you should because: 1. Might take less time than waiting for an answer. 2. Compiler is never cranky or in a bad mood. At most it will issue an error message. 3. One wild guess leads to another and soon you end up with a nice brilliant idea :)
Mircea
-
Calin Cali wrote:
Usually I don`t try to compile wild guesses.
Well, you should because: 1. Might take less time than waiting for an answer. 2. Compiler is never cranky or in a bad mood. At most it will issue an error message. 3. One wild guess leads to another and soon you end up with a nice brilliant idea :)
Mircea
Quote:
Well, you should because:
For me it feels like trying to compile a quote from Ceaikovski or Twain.
-
Quote:
Well, you should because:
For me it feels like trying to compile a quote from Ceaikovski or Twain.
Mircea is quite correct, you should always try and build small samples. Apart from anything else, the error reports help you to learn. The compiler, linker and debugger excellent tools that aid in development. Had you started in the days when we had to submit a deck of punched cards and wait 24 hours for the compiler output to tell us one character was mistyped, you would appreciate how easy things are these days.
-
Mircea is quite correct, you should always try and build small samples. Apart from anything else, the error reports help you to learn. The compiler, linker and debugger excellent tools that aid in development. Had you started in the days when we had to submit a deck of punched cards and wait 24 hours for the compiler output to tell us one character was mistyped, you would appreciate how easy things are these days.
It`s interesting to hear where things started. I had my first computer experience in the 80`s on a computer with keyboard and a dedicated green and white screen (the screen was displaying only two colors). My first programming experience was on a computer that you had to connect to a TV set. It was a computer with 16 colors graphics.
-
It`s interesting to hear where things started. I had my first computer experience in the 80`s on a computer with keyboard and a dedicated green and white screen (the screen was displaying only two colors). My first programming experience was on a computer that you had to connect to a TV set. It was a computer with 16 colors graphics.
Mine was the LEO* III in the 60s, second row first three pictures at Leo Computers Society. Leo 3 photos[^]. Most input was punched paper tape, and some punched cards, no mass storage, only magnetic tape. *The Lyons Electronic Office, initially developed between Lyons Catering and English Electric, both companies long gone.
-
Mine was the LEO* III in the 60s, second row first three pictures at Leo Computers Society. Leo 3 photos[^]. Most input was punched paper tape, and some punched cards, no mass storage, only magnetic tape. *The Lyons Electronic Office, initially developed between Lyons Catering and English Electric, both companies long gone.
Working in the field since the beginning I imagine you had a computer from every generation.
-
How do you use a pointer to a function that is declared in the general scope within a class. Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?
void somefunction(int i);
void (*pointertosomef)(int i) = somefunction;class SomeObject
{
void (*m_pointertosomef)(int i);
public:
void ImportPointerToF(void (*pointertosomef)(int i));
void UsePointerToF();
}void SomeObject::ImportPointerToF(void (*pointertosomef)(int i))
{
m_pointertosomef = pointertosomef;
}
void SomeObject::UsePointerToF()
{
m_pointertosomef(30);
}I found on the internet the basic version of declaring a pointer function
void fun(int a);
void (*fun_ptr)(int) = fun;
fun\_ptr(10);
Everything else is my speculation
You're creating a pointer to a free function (one defined outside a class) and then storing it, and invoking it, from within an object. But it's also possible to create a pointer to class member data or a member function. See here[^]. You may need to read several articles about this to gain a good understanding, because I can't point to one that is really good on its own. Search on "C++ pointer to member" and read articles that discuss the type
Class::*
(a pointer to a class member) and operators.*
and->*
. These make the following possible:int Class::\* pm = &Class::m; // pointer to member data "int m" int (Class::\* pf)(int) = &Class::f; // pointer to member function "int f(int)" Class c, \*k; c.\*pm = 1; // c.m = 1; n = c.\*pf(0); // n = c.f(0); k->.pm = 2; // k->m = 2; n = (k->\*pf)(0); // n = k->f(0); note that ->\* requires parentheses
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Working in the field since the beginning I imagine you had a computer from every generation.