Solved

pointer to member function

Posted on 1998-10-21
6
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 22

Accepted Solution

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

Expert Comment

by:nietod
ID: 1175718
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
ID: 1175719
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

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

Author Comment

by:vinny5
ID: 1175721
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
ID: 1175722
de nada
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

752 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