By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Ptrs to member funcs & inheritance

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:3F**************@sbcglobal.net...
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.

Jul 22 '05 #2

P: n/a
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:3F**************@sbcglobal.net...
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

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.