Solved

problem with a pointer to a non-static member function...

Posted on 2000-02-19
37
237 Views
Last Modified: 2011-10-03
What is the proper way to code a pointer to a non-static member function?
0
Comment
Question by:ipocina
  • 9
  • 9
  • 6
  • +6
37 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2538422
Add the class name before function,eg:

typedef void (A::*clsfnp)(int);
0
 
LVL 22

Expert Comment

by:nietod
ID: 2538427
Ouch.  Its ugly

Class SomeClass
{
public:
   int Add(int i, int j) { return i+j; };
   int Sub(int i, int j) { return i-j; };
};

// This typedef is not essential, but it
// makes it much easier to read.
typedef int (SomeClass::*MemFunPtrTyp)(int i, int j);

SomeClass A;
MemFunPtrTyp Ptr = &SomeClass::Add;
int x = A.*Ptr(1,2); // x = 3 = 1 + 2;

Let me know if you have any questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2538428
Beat me to it.
0
 

Author Comment

by:ipocina
ID: 2538430
Now let's put a twist on the problem:

What happens if the pointer to a non-static member function is ITSELF a member to the same class?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2538435
Btw:
You can initialize it as:


clsfnp Afnp=A::fn;

A a;
(a.*Afnp)(1);
0
 

Author Comment

by:ipocina
ID: 2538448
This is basic pointer-to-a-non-static member-function semantics!!!  I've put a twist to the problem.  (See above).
0
 

Author Comment

by:ipocina
ID: 2538452
Adjusted points to 200
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2538460
LOL,do you notice the time of my comment and your twist in between.
It's so unpolite to do this when you ask another question and reject the original question answer which is right.
Right,nietod?

>>What happens if the pointer to a non->>static member function is ITSELF a >>member to the same class?

o? What do you mean by "what happen"?
if you declare that way.It acts that way.

0
 

Author Comment

by:ipocina
ID: 2538468
I thought you people are (so-called) EXPERTS!!!
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2538475
>>I thought you people are (so-called) >>EXPERTS!!!

Dont use "are" above.

If you discriminate against me.Doesnt matter 'cause I'm very young student, and I deserve that to some degree.

But the other one is definitely SOMEONE and sure much better than you!!

Dont insult together.You are so uncultured!

Good luck to you as you wish.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2538481
You ask a question which I think I can solve,then I answer it.

So straightforward.


0
 
LVL 5

Expert Comment

by:Wyn
ID: 2538498
About the twist.
YOu can declare one member like:

void (*fnp)(...)

and initialize it as null in constructor and later fill it by the this pointer.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2538499
>> Right,nietod?
Very much.

ipocina, you asked a a question and wyn answered.  They you rejected the answer and asked a new question.  Your new request was not presented in the original question, that is not fair.

Now you've put yourself in a position where are not likely to get help.  What guarantee do we have that if we answer your question you won't reject it and ask another?
0
 
LVL 3

Expert Comment

by:Try
ID: 2539196
What happens if the pointer to a non-static member function is ITSELF a member to the same class?

---------------------------------

The 'this' pointer is an implicit member to every class, and can be used to point to any data member or member function of that class.  You don't need to explicitly define a pointer to point to a member function of the same class.

The only thing I might want to add to "Wyn's" answer (which is correct, BTW) is that you pay attention to the 'function type' of a pointer to a function.

A 'function type' is determined only by its (i.e. the function's) return type and its parameter list.  A function name is not part of its type.

"Wyn's" example shows a function type of 'void' and '(int)', whereas "nietod's" example shows a function type of 'int' and '(int, int)'.

I don't know why you rejected "Wyn's" answer.  I would urge you to reconsider and award him/her the points.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2539616
>> The 'this' pointer is an implicit member to
>> every class, and can be used to point to
>> any data member or
>> member function of that class.
You are missing the point.  A pointer-to-member is not related to the "this" pointer.  The question is how to define a pointer-to-a-member-function of a class WITHIN that class.  And if you know the answer (which you probably do) I would appreciate if you DIDN'T answer until ipocina agrees to award the points to wyn.
0
 
LVL 3

Expert Comment

by:Try
ID: 2539758
I see your point both ways.
0
 

Expert Comment

by:bertp
ID: 2540942
IMHO its a gross demonstration of lack of appreciation!
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2541881
>> lets put a twist ...
Not nice at all to reject Wyn's correct answer. You could have put in a comment.

You want a member of the class to hold a pointer to a member function?
0
Highfive Gives IT Their Time Back

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
ID: 2542040
>> You could have put in a comment
Really, it shoudl have been in the quesiton.  Its not fair to change the question after its been answered.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2542108
Agree, I just pointed out that a discussion may continue or broaden on the subject after the original question has been answered. ipocina would probably have had his second question answered even after accepting Wyn's correct and \emphasize{complete} answer.
0
 
LVL 4

Expert Comment

by:pellep
ID: 2542167
hmmm...
Doesn't a non-static member take 'this' as an implicit argument. Shouldn't the declaration of the pointer include an argument of the class-type?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2542319
The class type and the fact that it concerns a (non static) member function is known from the declaration typedef void(MyClass::*PMF)();
0
 
LVL 4

Accepted Solution

by:
Oliver_Dornauf earned 200 total points
ID: 2542705
Pointer to non static member funtions are easy.

First you class:
class test {
 public:
   test() {;} // do nothing in example
   ~test() {;} // dito

    typedef void (test::pmf)();
      // signatur of your memberfunktion
    typedef int (test::pmf2)(int);
      // signatur of a second ..

     void function1() { return;}
     int  function2(int ) {return 1;}
};

Now use it:
1) instance auf class
 test* x;
 x = new test;

2) Define pointers:
 test::pmf functionPtr1;
 test::pmf2 functionPtr2;

3) assign pointers
 functionPtr1 = &test::function1;
 functionPtr2 = &test::function2;

4) call the function in instance x of test

(x->*functionPtr1)()
(x->*functionPtr2)(10);


 
 
0
 
LVL 22

Expert Comment

by:nietod
ID: 2542755
Oliver, have you read the question history?

wyn already said that and his answer was rejected becuase ipocina unfairly asked a 2nd question.  We wanted ipocina to accept wyn's answer before we gave him the answer to his question.  That way the points would go to the expert deservign them.  Now they will go to you or to no one if ipocina rejects the answer--and there is nothing to prevent that.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2542853
Besides, IMO it's not correct:

>> typedef void (test::pmf)();
Should be typedef void(test::*pmf)();

>> typedef int (test::pmf2)(int);
should be typedef int(test::*pmf2)(int);

I admit that I am surprised that one can use your typedef with non members:

typedef void(F)();
typedef void(*F)();

but for class members only the pointer version is allowed:
typedef void(A::*pmf)();
0
 
LVL 22

Expert Comment

by:nietod
ID: 2543026
That is probably because pointers are so closely tied with functions (like arrays and pointers).  For exampel you don't have to place a "&" in front of a function name to create a pointer to the function.  (with/or without the "&" it acts like a pointer.)  You don't have to dereference a function pointer with "*" to call the function.  Again with or without the "*" is okay.  Another area where C++ should have departed from C...
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2546179
Oliver_Dornauf ,is this funny?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2546197
This has gotten out of hand, I'm going to notify customer service.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2546289
It should be my chores.
Thank you ,nietod.
0
 

Author Comment

by:ipocina
ID: 2546640
Very good!!!  Just what I was looking for!!!
0
 

Expert Comment

by:bertp
ID: 2546680
Hey wasn't this question previously worth more than 20 points?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2546703
Dont be self-abandoned....
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2546717
 5.3.1  Unary operators                                 [expr.unary.op]

3 A pointer to member is only formed when an explicit & is used and  its
  operand  is  a  qualified-id not enclosed in parentheses.  [Note: that
  is, the expression &(qualified-id), where the qualified-id is enclosed
  in  parentheses,  does not form an expression of type "pointer to mem-
  ber."  Neither does qualified-id, because there is no implicit conver-
  sion  from  a qualified-id for a nonstatic member function to the type
  "pointer to member function" as there is from an  lvalue  of  function
  type to the type "pointer to function" (_conv.func_).  Nor is &unqual-
  ified-id a pointer to member, even within the scope  of  the  unquali-
  fied-id's class.  ]
0
 

Author Comment

by:ipocina
ID: 2546756
I've tested Oliver's proposed answer and it REALLY works as is!!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2546829
You just awarded the points to Oliver_Dornauf.  But wyn had answered the question and rejected it.  Don't you understand that?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2546875
It may work, but it is not correct, meaning that it may fail to compile on other compilers.
Secondly, Wyn had answered the question. If you did not understand his answer you could have asked for explantion. It is near impossible to estimate beforehand how much explanation is needed without feedback from you. It is importa
Thirdly, you changed (twisted) the question after it had been answered, which just isn't fair.  The little twist should have been handled differently.
0
 
LVL 3

Expert Comment

by:darinw
ID: 2547482
Wyn please see the question I have posted for you in this topic area:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10299289

darinw
Customer Service
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 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.

707 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

17 Experts available now in Live!

Get 1:1 Help Now