473,769 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(vo id) const;
};

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

class Book
: public Published
{
string author;
string isbn;
protected:
string to_str_author(v oid) const;
string to_str_isbn(voi d) 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.l earn.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 1677

"Thomas Matthews" <Th************ *************@s bcglobal.net> wrote in
message news:3F******** ******@sbcgloba l.net...
In the above example, all the member functions have the signature:
string {function_name} (void) const;


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

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

typedef string (ClassName::*po inter_type)() const;

You form the pointer from the qualified name:
&ClassName::fun ction;

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************ *************@s bcglobal.net> wrote in
message news:3F******** ******@sbcgloba l.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
1139
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
3488
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 non-zero index is returned. Below is my attempt, which generates compiler errors: namespace Reference {
8
1854
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 what arguments do you decide whether to pass a reference to the object of class B to the member function like this operator()(classB& objB) or to have in class A a data member (a const pointer to a class B object or so) and have this set during...
37
5021
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
3
1189
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
1929
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
4410
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 same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
20
2166
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 mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
3
1445
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 equivalently if x is a member of a struct, class, single inheritance hierarchy, or multiple inheritance hierarchy" This is because compiler knows the offset of
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.