Solved

pointer to member function

Posted on 1998-10-21
6
201 Views
Last Modified: 2010-04-02
I am trying to create a function pointer in my class
that will point to different member function within the
same class.

My problem is that I cant figure out how to call the
function pointer.

Here is a very simple example:

class CDevice
{
public:
      CDevice();
      typedef void (CDevice::* EraseFnPtr)(int i);
      EraseFnPtr Erase;

      void Erase1(int i);
      void Erase2(int i);
};

CDevice::CDevice()
{
      Erase = &CDevice::Erase1;
}

void CDevice::Erase1(int i)
{
      printf("Erase1 %d\n",i);
}


void main()
{
      CDevice Device;
      (Device.*Erase)(34);
}

I want the program to print out "Erase1 34".

But I get 2 compile errors that say
error C2065: 'Erase' : undeclared identifier
error C2297: '.*' : bad right operand

both regarding to this line:
----->      (Device.*Erase)(34);
This is in Visual C++ 5.0

So How do I call the Erase function pointer?
thanks

PS I dont want to change the way the class is set up, In other words I want the function pointer to be within the CDevice class.

0
Comment
Question by:vinny5
  • 5
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 170 total points
Comment Utility
Problem: erase is a member.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
In

(Device.*Erase)(34);

the compiler is looking for a local or global variable called "Erase".  But there is none.  The erase you want is a member of the class try

(Device.*Defice.Erase)(34);

Or better yet, define a member function that does the calling using the pointer.  Then call that member function here.  That hides the pointer-to-member stuff inside the class.   (Where I would say it belongs.  In most cases, I think I would make it entirely private to the class.)

Let me know if you have questions.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Wait a second.

>> PS I dont want to change the way the class is set up, In other words
>> I want the function pointer to be within the CDevice class

You don't want it to be within the class?  but you declared it there!  You can declare it outside the class, if that is what you want.   (But I wouldn't recommend it.)  I would move the typedef outside as well, in that case.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:nietod
Comment Utility
Opps, ignore that last comment.  In my mind I added a "don't" that wasn't there.
0
 

Author Comment

by:vinny5
Comment Utility
BAM!

thanks neitod...
Im sorting of doing what you recommended I was just trying to create a simple
example to illustarte my question..  but  (Device.*Device.Erase)(34);
was exactly what I was looking for.

gracias

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
de nada
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now