Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

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

What is the proper way to code a pointer to a non-static member function?
0
ipocina
Asked:
ipocina
  • 9
  • 9
  • 6
  • +6
1 Solution
 
WynCommented:
Add the class name before function,eg:

typedef void (A::*clsfnp)(int);
0
 
nietodCommented:
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
 
nietodCommented:
Beat me to it.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ipocinaAuthor Commented:
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
 
WynCommented:
Btw:
You can initialize it as:


clsfnp Afnp=A::fn;

A a;
(a.*Afnp)(1);
0
 
ipocinaAuthor Commented:
This is basic pointer-to-a-non-static member-function semantics!!!  I've put a twist to the problem.  (See above).
0
 
ipocinaAuthor Commented:
Adjusted points to 200
0
 
WynCommented:
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
 
ipocinaAuthor Commented:
I thought you people are (so-called) EXPERTS!!!
0
 
WynCommented:
>>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
 
WynCommented:
You ask a question which I think I can solve,then I answer it.

So straightforward.


0
 
WynCommented:
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
 
nietodCommented:
>> 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
 
TryCommented:
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
 
nietodCommented:
>> 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
 
TryCommented:
I see your point both ways.
0
 
bertpCommented:
IMHO its a gross demonstration of lack of appreciation!
0
 
KangaRooCommented:
>> 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
 
nietodCommented:
>> 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
 
KangaRooCommented:
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
 
pellepCommented:
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
 
KangaRooCommented:
The class type and the fact that it concerns a (non static) member function is known from the declaration typedef void(MyClass::*PMF)();
0
 
Oliver_DornaufCommented:
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
 
nietodCommented:
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
 
KangaRooCommented:
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
 
nietodCommented:
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
 
WynCommented:
Oliver_Dornauf ,is this funny?
0
 
nietodCommented:
This has gotten out of hand, I'm going to notify customer service.
0
 
WynCommented:
It should be my chores.
Thank you ,nietod.
0
 
ipocinaAuthor Commented:
Very good!!!  Just what I was looking for!!!
0
 
bertpCommented:
Hey wasn't this question previously worth more than 20 points?
0
 
WynCommented:
Dont be self-abandoned....
0
 
KangaRooCommented:
 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
 
ipocinaAuthor Commented:
I've tested Oliver's proposed answer and it REALLY works as is!!!
0
 
nietodCommented:
You just awarded the points to Oliver_Dornauf.  But wyn had answered the question and rejected it.  Don't you understand that?
0
 
KangaRooCommented:
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
 
darinwCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 9
  • 6
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now