Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dot asterisk operator in c++

Tags:

c++

is there, and if, what it does?

.* 
like image 720
hash Avatar asked Mar 30 '10 20:03

hash


People also ask

What is Dot Star operator?

It's the pointer-to-member operator for use with pointer-to-member types. E.g.

What is a dot operator in C?

(dot) operator is used to access class, structure, or union members. The member is specified by a postfix expression, followed by a . (dot) operator, followed by a possibly qualified identifier or a pseudo-destructor name.

What is dot operator and arrow operator in C?

The Dot(.) operator is used to normally access members of a structure or union. The Arrow(->) operator exists to access the members of the structure or the unions using pointers.

What is -> in C programming?

The dot ( . ) operator is used to access a member of a struct, while the arrow operator ( -> ) in C is used to access a member of a struct which is referenced by the pointer in question.


2 Answers

Yes, there is. It's the pointer-to-member operator for use with pointer-to-member types.

E.g.

struct A {     int a;     int b; };  int main() {     A obj;     int A::* ptr_to_memb = &A::b;      obj.*ptr_to_memb = 5;      ptr_to_memb = &A::a;      obj.*ptr_to_memb = 7;      // Both members of obj are now assigned } 

Here, A is a struct and ptr_to_memb is a pointer to int member of A. The .* combines an A instance with a pointer to member to form an lvalue expression referring to the appropriate member of the given A instance obj.

Pointer to members can be pointers to data members or to function members and will even 'do the right thing' for virtual function members.

E.g. this program output f(d) = 1

struct Base {     virtual int DoSomething()     {         return 0;     } };  int f(Base& b) {     int (Base::*f)() = &Base::DoSomething;     return (b.*f)(); }  struct Derived : Base {     virtual int DoSomething()     {         return 1;     } };  #include <iostream> #include <ostream>  int main() {     Derived d;     std::cout << "f(d) = " << f(d) << '\n';     return 0; } 
like image 167
CB Bailey Avatar answered Oct 02 '22 16:10

CB Bailey


You may come across that operator when using member pointers:

struct foo {     void bar(void); };  typedef void (foo::*func_ptr)(void);  func_ptr fptr = &foo::bar; foo f;  (f.*fptr)(); // call 

Also related is the ->* operator:

func_ptr fptr = &foo::bar; foo f; foo* fp = &f;  (fp->*fptr)(); // call 
like image 23
GManNickG Avatar answered Oct 02 '22 17:10

GManNickG