Thread: operator

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    operator

    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

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    well actually it is .*;

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    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

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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:
    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;
    }
    That was shamelessly ripped from examples off of http://www.newty.de/fpt/index.html. They've got a good tutorial on function pointers.
    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

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by pianorain
    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:
    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;
    }
    That was shamelessly ripped from examples off of http://www.newty.de/fpt/index.html. They've got a good tutorial on function pointers.
    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.

    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.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by pianorain
    I won't dispute that it looks weird, but .* and ->* are both seperate operators of themselves.
    Just checked, it falls under pointer to member operators, which are seperate.
    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.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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:
    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;
    }
    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* 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM