Solved

pointer to member function

Posted on 1998-10-21
6
202 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cb: unreferenced local variable 11 72
TTimer no longer functioning 7 84
Getting IP address 8 71
Need some help with Microsoft Visual Studio C++ 2003 5 51
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

14 Experts available now in Live!

Get 1:1 Help Now