473,665 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointers to member functions

Dear all,

I'm fairly new to C++ so forgive me is this topic may have been
convered before... I'm having a problem with pointers to member
functions. Essentially what I want to achieve is the following:

class A {
public:
double (A::*ptrToFunct ion)(double);
}

// and NOTHING else is in class A.

class B : public A {
public:
double function1(doubl e);
double function2(doubl e);
double function3(doubl e);
}

// and NOTHING else is in class B.

Later on, I would like to do the following:

class ApplyCoolFuncti ons {
public:
double apply(A &a);
}

where

ApplyCoolFuncti ons::apply(A *a) {
a->*ptrToFunction (20.0);
}

The idea being that I can feed in objects of class B to
ApplyCoolFUncti ons, i.e. with calls like:

B b;
b.ptrToFunction = &B::function 1;
ApplyCoolFuncti ons(&b);
b.ptrToFunction = &B::funcion2 ;
ApplyCoolFuncti on(&b);

But... it doesn't work!! Is what I'm doing even possible? Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.

Any help with be greatly appreciated.

Thanks,
Jolie

Oct 31 '05 #1
7 2170
jo************* @gmail.com wrote:
B b;
b.ptrToFunctio n = &B::function 1;
ApplyCoolFunct ions(&b);
b.ptrToFunctio n = &B::funcion2 ;
ApplyCoolFunct ion(&b);

But... it doesn't work!! Is what I'm doing even possible? Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.


At first glance it looks like you would need to declare
function1/function2/function3 as static for this to work. You have
them defined as class member functions which cannot be assigned in
this way.

This should hopefully give you a starting point to help you on your
way to finding out what you should be doing.

Oct 31 '05 #2
jo************* @gmail.com wrote:
Dear all,

I'm fairly new to C++ so forgive me is this topic may have been
convered before... I'm having a problem with pointers to member
functions. Essentially what I want to achieve is the following:

class A {
public:
double (A::*ptrToFunct ion)(double);
}

// and NOTHING else is in class A.

class B : public A {
public:
double function1(doubl e);
double function2(doubl e);
double function3(doubl e);
}

// and NOTHING else is in class B.

Later on, I would like to do the following:

class ApplyCoolFuncti ons {
public:
double apply(A &a);
}

where

ApplyCoolFuncti ons::apply(A *a) {
a->*ptrToFunction (20.0);
}

The idea being that I can feed in objects of class B to
ApplyCoolFUncti ons, i.e. with calls like:

B b;
b.ptrToFunction = &B::function 1;
ApplyCoolFuncti ons(&b);
b.ptrToFunction = &B::funcion2 ;
ApplyCoolFuncti on(&b);

But... it doesn't work!! Is what I'm doing even possible? Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.

Any help with be greatly appreciated.

Thanks,
Jolie


Two errors

1) Syntax should be

(a->*(a->ptrToFunction) )(20.0);

2) Concept doesn't work. Consider B::function1, that is a member
function which must be called with a B object or an object derived from
B. If you could assign it to a pointer to member function of A, then it
would possible to call a B member function with an A object. So the
assignment is not allowed.

john
Oct 31 '05 #3
jo************* @gmail.com wrote:
I'm fairly new to C++ so forgive me is this topic may have been
convered before...
It's also probably covered in the FAQ Lite. Have you read it?
I'm having a problem with pointers to member
functions. Essentially what I want to achieve is the following:

class A {
public:
double (A::*ptrToFunct ion)(double);
} ;

// and NOTHING else is in class A.

class B : public A {
public:
double function1(doubl e);
double function2(doubl e);
double function3(doubl e);
} ;

// and NOTHING else is in class B.

Later on, I would like to do the following:

class ApplyCoolFuncti ons {
public:
double apply(A &a);
} ;

where

ApplyCoolFuncti ons::apply(A *a) {
That's

double ApplyCoolFuncti ons::apply(A &a) {

(why'd you switch the argument type and lost the return value type?)
a->*ptrToFunction (20.0);
That's not how you call it. Should be

(a.*ptrToFuncti on)(20.0);

('a' is actually a reference and the syntax requires the parentheses).
}

The idea being that I can feed in objects of class B to
ApplyCoolFUncti ons, i.e. with calls like:

B b;
b.ptrToFunction = &B::function 1;
That's not going to fly. You are trying to assign a member of B to
a pointer to a member of A. The language only defines the _reverse_
as an implicit conversion. Any member of A is a member of B, but not
vice versa.
ApplyCoolFuncti ons(&b);
b.ptrToFunction = &B::funcion2 ;
ApplyCoolFuncti on(&b);

But... it doesn't work!!
Read the FAQ 5.8.
Is what I'm doing even possible?
Depending on what you're trying to accomplish. Are you attempting some
kind of polymorphic behaviour without using virtual functions? It seems
so, but I cannot be sure. Why not use virtual functions?
Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.


You could try using static_cast:

b.ptrToFunction = static_cast<dou ble(A::*)(doubl e)>(&B::functio n2);

.... But I am still not convinced that it's the best approach.

V
Oct 31 '05 #4
* jo************* @gmail.com:

I'm fairly new to C++ so forgive me is this topic may have been
convered before... I'm having a problem with pointers to member
functions. Essentially what I want to achieve is the following:

class A {
public:
double (A::*ptrToFunct ion)(double);
}

// and NOTHING else is in class A.

class B : public A {
public:
double function1(doubl e);
double function2(doubl e);
double function3(doubl e);
}

// and NOTHING else is in class B.

Later on, I would like to do the following:

class ApplyCoolFuncti ons {
public:
double apply(A &a);
}

where

ApplyCoolFuncti ons::apply(A *a) {
a->*ptrToFunction (20.0);
}
A class is not a function. Don't think of a class as "doing" something.
A class can model a function, but you don't need that.

The idea being that I can feed in objects of class B to
ApplyCoolFUncti ons, i.e. with calls like:

B b;
b.ptrToFunction = &B::function 1;
ApplyCoolFuncti ons(&b);
b.ptrToFunction = &B::funcion2 ;
ApplyCoolFuncti on(&b);

But... it doesn't work!! Is what I'm doing even possible? Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.

Any help with be greatly appreciated.


You need ordinary function pointers or virtual member functions, not
pointers to member functions.

It's hard to figure out what you're trying to do since most of the above
is concerned with an attempt at a technical solution to ... something.

What exactly is it you want applyCoolFuncti ons() to do?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 31 '05 #5
>
2) Concept doesn't work. Consider B::function1, that is a member
function which must be called with a B object or an object derived from
B. If you could assign it to a pointer to member function of A, then it
would possible to call a B member function with an A object. So the
assignment is not allowed.


You would be OK if A derived from B, but not the other way around.

john
Oct 31 '05 #6
Essentially what I am trying to do is copy the idea of a "foldr"
function or something like a "map" function, which takes a function as
a parameter and then applies to recursively/iteratively to to some data
structure.

SO, for example, if I defined the function

double add2(double d) {
return d+2.0;
}

and then I would like to do the following: map(add2) listofDoubles;
where listOfDoubles is an array of doubles.

Anyway, I'm not really after a map, but the same principle applies. I
want to take a function as a parameter and I want want to work through
a kind of tree applying the function supplied to each node in the tree.

Now, the idea is that there are several classes I want to implement,
each of which can list a number of functions.

For example, class A might have 12 functions it needs to apply, class B
has 2, class C might have 4 and so forth. So, what I was hoping is that
A, B, C could all have base class X, where X basically just contained a
function pointer, and then, inside A, for example, I could get the
function pointer (inherited from X) to point to a different function
defined in A whenever I wanted to.

The idea is then that there is some other class which contains a
function
applyCoolFuncti on(X *x) {
}
and then it would take an X object and based on what X's pointer
function is pointing to apply that function...?

So inside class A, i would at some point
make the functionpointer point to function1, then call
applyCoolFuncti on(this);
then later on I would make the functionpointer point to function2, then
call
applyCoolFuncti on(this);
and so forth.

BY the way, none of the functions defined in class A , B or C are meant
to be static.

THanks,
Jolie

Oct 31 '05 #7
* jo************* @gmail.com:
Essentially what I am trying to do is copy the idea of a "foldr"
function or something like a "map" function, which takes a function as
a parameter and then applies to recursively/iteratively to to some data
structure.

SO, for example, if I defined the function

double add2(double d) {
return d+2.0;
}

and then I would like to do the following: map(add2) listofDoubles;
where listOfDoubles is an array of doubles.
#include <algorithm> // std::copy, std::transform
#include <iterator> // std::ostream_it erator
#include <iostream> // std:.cout
#include <ostream> // <<, std::endl
#include <vector> // std::vector

void display( std::vector<int > const& v )
{
std::copy(
v.begin(), v.end(),
std::ostream_it erator<int>( std::cout, " " )
);
std::cout << std::endl;
}

int add2( int x ) { return x+2; }

int main()
{
static int const data[] = {1, 2, 3};
std::vector<int > v( data, data + 3 );

display( v );
std::transform( v.begin(), v.end(), v.begin(), add2 );
// std::transform is the C++ "apply".
display( v );
}
You know, this is rather interesting. For when I replace 'int' with
'double' my trusty old Microsoft Visual C++ 7.1 compiler says:

fatal error C1001: INTERNAL COMPILER ERROR (compiler file
'f:\vs70builds\ 3077\vc\Compile r\Utc\src\P2\ma in.c', line 148)

And guess what, it's the old 'int main' issue raring its ugly head.

For my editor of course changed that to 'double main' -- so now I know
the two thousand and forty third way of ICEing this compiler...

Anyway, I'm not really after a map,
Any function is a map, isn't it?

but the same principle applies. I
want to take a function as a parameter and I want want to work through
a kind of tree applying the function supplied to each node in the tree.
You can use a function pointer or an object with a virtual member
function or a functor object, but don't use member function pointers.

E.g.,

typedef double FuncFoo( double x );

void apply( Node& node, FuncFoo foo )
{
node.data = foo( node.data );
apply( *node.left, foo );
apply( *node.right, foo );
}

Now, the idea is that there are several classes I want to implement,
each of which can list a number of functions.

For example, class A might have 12 functions it needs to apply, class B
has 2, class C might have 4 and so forth. So, what I was hoping is that
A, B, C could all have base class X, where X basically just contained a
function pointer, and then, inside A, for example, I could get the
function pointer (inherited from X) to point to a different function
defined in A whenever I wanted to.


?

I think you'd better forget that idea for a solution. Don't think in
terms of changing function pointers. Just supply the relevant functions
or objects or whatever as arguments to your 'apply' function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 1 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1486
by: xuatla | last post by:
Hi, I have the following code for function pointer. compiling is ok. Can you help me to check whether it's a good way to implement as: class CA { ..... private: void f1( double ) ;
4
2263
by: Ian Malone | last post by:
I have a class which looks like this: class reco { public: int height, width; double beta; double mu; simple_d_field estimate; simple_d_field auxiliary;
3
6693
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? Thanks, Bahadir
11
4410
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
12
2005
by: addinall | last post by:
Hi guys and gals, Haven't been in here for a good decade or so! I'm having a braid-dead moment that is lasting for a couple of days. Declaring pointers to functions witrhin structures and accessing same.... This doesnt work. I thought it should. The compiler disagrees with me! gcc32
5
3174
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++ class which has several virtual functions and several member variables, will the default copy constructor also copy the virtual table pointer, or will it merely do a (shallow) copy of the member variables? I'm using gcc 4.1.1
22
3165
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my header it looks like this (private section) vector<Directory*Directories;
12
2856
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair of pointers, where each is a pointer to the base class (each is a SPRITE *). I know that each of these pointers actually points to one of the derived classes, even though the type of the pointer is SPRITE *, but I don't know which derived class it...
3
2816
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member function pointer array (Handlers) - compiler doesnt let me do that. I got an error as below: "invalid use of non-static member function bool test::func1(long int)"
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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
8863
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
8636
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...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.