Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pointer to member function

Posted on 1998-10-21
6
Medium Priority
?
209 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 680 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

877 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