Pointers to Members and Functions

Pointers to Members and Functions - Pointers to Members and...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Pointers to Members and Functions POINTERS TO MEMBERS In ANSI C, function pointers are used like this: #include <stdio.h> void f(int i) { printf("%d\n", i); } typedef void (*fp)(int); void main() { fp p = &f; (*p)(37); /* these are equivalent */ p(37); } and are employed in a variety of ways, for example to specify a comparison function to a library function like qsort(). In C++, pointers can be similarly used, but there are a couple of quirks to consider. We will discuss two of them in this section, and another one in the next section. The first point to mention is that C++ has C-style functions in it, but also has other types of functions, notably member functions. For example: class A { public: void f(int); }; In this example, A::f(int) is a member function. That is, it operates on object instances of class A, and the function itself has a "this" pointer that points at the instance in question. Because C++ is a strongly typed language, it is desirable that a pointer to a member
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

Pointers to Members and Functions - Pointers to Members and...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online