what does this operator do .*
i've never seen it before all what i know about it is that it can not be overloaded .
anyhelp would be appreciated
what does this operator do .*
i've never seen it before all what i know about it is that it can not be overloaded .
anyhelp would be appreciated
Due to your improper use of punctuation, it is difficult to see if you're referring to '*', to '.', or to '.*;, which incidentally, does not exist. It seems most likely you are referring to '*'.
This operator is used for working with pointers (declaring them and dereferencing them), and also for multiplication. I believe it can be overloaded.
well actually it is .*;
You might have seen that sequence used to dereference a member in a structure, in which case it is actually two operators, but there wouldn't have been a semicolon after it. Post the code in which you saw this.
i've got a table in my book for the operators that can not be overloaded and it is one of them , all the other operator im very familiar with them like '::' ,'?:' but '.*' is the new one , besides there is no explanation for it in the book
Actually, '.*' is an operator. Both .* and ->* are related. You use .* to access the member function of a class from an object of that class and a function pointer. You use ->* to access the member function of a class from a pointer to an object of that class and a function pointer. Look at this example code:That was shamelessly ripped from examples off of http://www.newty.de/fpt/index.html. They've got a good tutorial on function pointers.Code:#include <iostream> using std::cout; using std::endl; class TMyClass { public: void DoIt(int a){ cout << "TMyClass::DoIt "<< a << endl;}; void DoMore(int a){ cout << "TMyClass::DoMore " << a << endl;}; }; int main() { void (TMyClass::*pt2Member)(int) = NULL; TMyClass instance1; pt2Member = &TMyClass::DoIt; (instance1.*pt2Member)(10); pt2Member = &TMyClass::DoMore; (instance1.*pt2Member)(20); TMyClass *instance2 = new TMyClass(); pt2Member = &TMyClass::DoIt; (instance2->*pt2Member)(10); pt2Member = &TMyClass::DoMore; (instance2->*pt2Member)(20); delete instance2; return 0; }
Last edited by pianorain; 03-08-2005 at 02:26 PM.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Acutally, I think both are 2 operators put together, as -> and . are one operator, and * is another. In this sense, I believe * is simply being use to dereference the pointer, and access the value (function in this case) its pointing to, not the pointer.Originally Posted by pianorain
Too add, that code looks weird as heck, although legal.
If any part of my post is incorrect, please correct me.
This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.
I won't dispute that it looks weird, but .* and ->* are both seperate operators of themselves.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Just checked, it falls under pointer to member operators, which are seperate.Originally Posted by pianorain
If any part of my post is incorrect, please correct me.
This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.
Hi,
My book concurs: it lists the operators that cannot be overloaded, and one of them is:
.*
which it says is the "de-reference pointer to class member operator". Here is a simple example of it in action:
Note that the operator .* is different than taking a class member that is a pointer and dereferencing it, which would be done like this:Code:#include <iostream> using namespace std; class Apple { public: int size; int weight; Apple(int size_, int weight_) { size = size_; weight = weight_; } }; int main() { int Apple::* pMembers; //pMembers can point to any int in Apple pMembers = &Apple::size; //pMembers set to point to the size member of Apple, //but it doesn't point to a specific value yet Apple a(3, 10); cout<<a.*pMembers<<endl; //operator .* in action return 0; }
Code:#include <iostream> using namespace std; class Apple { public: int* pointer; Apple(int* p) { pointer = p; } }; int main() { int i = 100; int* pi = &i; Apple a(pi); cout<<*(a.pointer)<<endl; return 0; }
Last edited by 7stud; 03-09-2005 at 02:48 AM.