Solved

pointer to member function

Posted on 1998-10-21
6
207 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
Technology Partners: 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

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!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

628 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