ipocina
asked on
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?
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.
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)
SomeClass A;
MemFunPtrTyp Ptr = &SomeClass::Add;
int x = A.*Ptr(1,2); // x = 3 = 1 + 2;
Let me know if you have any questions.
Beat me to it.
ASKER
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?
What happens if the pointer to a non-static member function is ITSELF a member to the same class?
Btw:
You can initialize it as:
clsfnp Afnp=A::fn;
A a;
(a.*Afnp)(1);
You can initialize it as:
clsfnp Afnp=A::fn;
A a;
(a.*Afnp)(1);
ASKER
This is basic pointer-to-a-non-static member-function semantics!!! I've put a twist to the problem. (See above).
ASKER
Adjusted points to 200
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.
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.
ASKER
I thought you people are (so-called) EXPERTS!!!
>>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.
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.
You ask a question which I think I can solve,then I answer it.
So straightforward.
So straightforward.
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.
YOu can declare one member like:
void (*fnp)(...)
and initialize it as null in constructor and later fill it by the this pointer.
>> 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?
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?
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.
--------------------------
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.
>> 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-functi on 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.
>> 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-functi
I see your point both ways.
IMHO its a gross demonstration of lack of appreciation!
>> 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?
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?
>> 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.
Really, it shoudl have been in the quesiton. Its not fair to change the question after its been answered.
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.
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?
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?
The class type and the fact that it concerns a (non static) member function is known from the declaration typedef void(MyClass::*PMF)();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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)();
>> 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)();
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...
Oliver_Dornauf ,is this funny?
This has gotten out of hand, I'm going to notify customer service.
It should be my chores.
Thank you ,nietod.
Thank you ,nietod.
ASKER
Very good!!! Just what I was looking for!!!
Hey wasn't this question previously worth more than 20 points?
Dont be self-abandoned....
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. ]
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. ]
ASKER
I've tested Oliver's proposed answer and it REALLY works as is!!!
You just awarded the points to Oliver_Dornauf. But wyn had answered the question and rejected it. Don't you understand that?
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.
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.
Wyn please see the question I have posted for you in this topic area:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10299289
darinw
Customer Service
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10299289
darinw
Customer Service
typedef void (A::*clsfnp)(int);