Ptrs to member funcs & inheritance

T

Thomas Matthews

Hi,

I have a hierarchy of classes and would like to create an array
of pointers to member functions that could also contain pointers
to the parent member functions as well.
What is the syntax for this?

#include <string>
using std::string;

class Base
{
string title;
protected:
string to_str_title(void) const;
};

class Published
: public Base
{
string publisher;
protected:
string to_str_publisher(void) const;
};

class Book
: public Published
{
string author;
string isbn;
protected:
string to_str_author(void) const;
string to_str_isbn(void) const;
};

In the above example, all the member functions have the signature:
string {function_name}(void) const;

I am creating a function that will return a field's data as a string
based on an index. I intend to have a vector of member functions
so that I can have each parent push their member function into the
vector. This eliminates the need for fixed magick numbers for the
field indices and not have to worry about the indices of parent
classes. (I am supply an interface to treat the classes as
records of fields.)

Questions:
0. The C++ FAQ section 30 does not discuss inheritance and
member functions. "The C++ Programming Language, Special
Edition", does not discuss inheritance of member functions
either.

1. What is the syntax for declaring a typedef to a member function
that allows for parent functions?

2. Where does the "const" go in the typedef for constant member
functions?

3. Can the member functions be private even though they are
accessed in a vector created by the leaf class?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
R

Ron Natalie

In the above example, all the member functions have the signature:
string {function_name}(void) const;

No, they have type
string ClassName::FunctionName() const;

The pointer type is
string (ClassName::*)() const;
the typename works just like defrining a variable by adding typedef.

typedef string (ClassName::*pointer_type)() const;

You form the pointer from the qualified name:
&ClassName::function;

You can get at the functions in the parent classes by just using the
appropriate qualified names. There is an implicit conversion between
pointer-to-base member to pointer-to-derived member.

One caveat, virtual function substitution occurs after the pointer
dereference
occurs, you can't get a handle to a particular implementaiton of a virtual
function.
 
D

David White

Thomas Matthews said:
Hi,

I have a hierarchy of classes and would like to create an array
of pointers to member functions that could also contain pointers
to the parent member functions as well.

Do you mean one array for each derived class or one array in total?
3. Can the member functions be private even though they are
accessed in a vector created by the leaf class?

Yes, but only the class the function belongs to or a friend can take the
function's address in the first place.

DW
 

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
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top