Function Pointers

M

Michael

Ok,
I understand the function pointers in C, but i'm a bit more confused in c++.

Firstly, as i understand it, a static member function can be
"function-pointed" to as a normal c function, but in order to pointer to f2,
an object must be instatiated. Is this correct??

class cl
{
public:
static void f1(int a) {}
void f2(int b) {}

};


Furthermore if I have a virtual function over-ridden in a derived class


class cl2
{
public:
virtual void a(int a){cout <<"BASE called"};
};


class Bcl2 : cl2
{
public:
void a(int a){cout <<"DERIVED called"};
};


cl2* ptr = new Bcl2;

now if I were to take a pointer to a of ptr, I assume that the 'vtables'
would correctly sort it out so i get the second "DERIVED called" function.
Is this right?






Lastly, what are functors, I any one knows of anywhere they are explained
simply and slowly I'd be grateful, I seem to keep getting bogged down wih
templates when I read them!!!

Thanks


Mike
 
K

Karl Heinz Buchegger

Michael said:
Ok,
I understand the function pointers in C, but i'm a bit more confused in c++.

Firstly, as i understand it, a static member function can be
"function-pointed" to as a normal c function, but in order to pointer to f2,
an object must be instatiated. Is this correct??

Yes

class cl
{
public:
static void f1(int a) {}
void f2(int b) {}

};

Furthermore if I have a virtual function over-ridden in a derived class

class cl2
{
public:
virtual void a(int a){cout <<"BASE called"};
};

class Bcl2 : cl2
{
public:
void a(int a){cout <<"DERIVED called"};
};

cl2* ptr = new Bcl2;

now if I were to take a pointer to a of ptr, I assume that the 'vtables'
would correctly sort it out so i get the second "DERIVED called" function.
Is this right?
Yes.


Lastly, what are functors, I any one knows of anywhere they are explained
simply and slowly I'd be grateful, I seem to keep getting bogged down wih
templates when I read them!!!

A functor is simply a class, which has an operator()
Thus objects of that class can be used, whenever syntactically a function
would be called.
 
R

Rolf Magnus

Michael said:
Ok,
I understand the function pointers in C, but i'm a bit more confused
in c++.

Firstly, as i understand it, a static member function can be
"function-pointed" to as a normal c function,

Not portably. A pointer to a C function must point to a function
declared as extern "C" in C++.
but in order to pointer to f2, an object must be instatiated. Is this
correct??

Yes. After all, it's a member function that is called on an object.
Without the object, calling a non-static member makes no sense. So you
can't let a normal function pointer point to it.
class cl
{
public:
static void f1(int a) {}
void f2(int b) {}

};


Furthermore if I have a virtual function over-ridden in a derived
class


class cl2
{
public:
virtual void a(int a){cout <<"BASE called"};
};


class Bcl2 : cl2
{
public:
void a(int a){cout <<"DERIVED called"};
};


cl2* ptr = new Bcl2;

now if I were to take a pointer to a of ptr, I assume that the
'vtables' would correctly sort it out so i get the second "DERIVED
called" function. Is this right?
Yes.

Lastly, what are functors, I any one knows of anywhere they are
explained simply and slowly I'd be grateful, I seem to keep getting
bogged down wih templates when I read them!!!

They are objects that behave like functions. They use the operator() to
make them "callable". Consider this:

#include <iostream>

struct square
{
double operator()(double in) const
{
return in*in;
}
};

int main()
{
square s;
std::cout << s(3) << std::endl;
}
 
G

Gert Van den Eynde

Michael said:
Ok,
I understand the function pointers in C, but i'm a bit more confused in
c++.

Firstly, as i understand it, a static member function can be
"function-pointed" to as a normal c function, but in order to pointer to
f2, an object must be instatiated. Is this correct??

class cl
{
public:
static void f1(int a) {}
void f2(int b) {}

};


Furthermore if I have a virtual function over-ridden in a derived class


class cl2
{
public:
virtual void a(int a){cout <<"BASE called"};
};


class Bcl2 : cl2
{
public:
void a(int a){cout <<"DERIVED called"};
};


cl2* ptr = new Bcl2;

now if I were to take a pointer to a of ptr, I assume that the 'vtables'
would correctly sort it out so i get the second "DERIVED called"
function. Is this right?






Lastly, what are functors, I any one knows of anywhere they are explained
simply and slowly I'd be grateful, I seem to keep getting bogged down wih
templates when I read them!!!

Thanks


Mike

You can find a great tutorial at http://www.function-pointer.org/
It got me going in no-time with function pointers and functors.

bye,
gert
 
D

David Harmon

On Fri, 14 May 2004 12:44:33 +0200 in comp.lang.c++, Karl Heinz
Buchegger said:
A functor is simply a class, which has an operator()
Thus objects of that class can be used, whenever syntactically a function
would be called.

Provided that the caller is flexible enough about the type of the thing
it is calling. In many cases, the caller is probably a template and the
type a template parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top