473,413 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Ptrs to member funcs & inheritance

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
2 1656

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Canfield | last post by:
Is fn2() accessible? class c: def fn1(self): print "in fn1()" def fn2(): print "in fn2()" Do member functions with no arguments have any meaning?
6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: puzzlecracker | last post by:
WHat is the difference between? Could someone explain how it's implemented, what goes on behind the scenes in latter case? thanks.. a) class base { public:
7
by: alex.mizrahi | last post by:
helo i'm going to use pointers to member function with virtual inheritance in following way: struct base { public: typedef void (base::*fun_t)(void); int i; void call(fun_t f) {
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
3
by: Rahul | last post by:
While reading Inside the C++ object model I came through the following paragraph Point3d origin, *ptr = &origin; A) origin.x = 0.0; B) ptr->x = 0.0; The book says "A & B statements performs...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.