470,826 Members | 1,788 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

operator overloand and .*

Hi,

I know that .* can not be overloaded but I am looking for some
background information for this. I have no need or desire to overload
it, but I am just curious from an educational point of view.

And what does this exactly refer to? Is it .* as in:

struct foo
{
int *bar;
}

int val = foo.*bar;

Or is there some other case when .* is used? Curious as to the reasons
why it can not be overloaded. I did not realize that .* is an operator
itself, I always thought of . * as two different operators (i.e. as in
the the above example).

Thanks

May 10 '06 #1
5 1431
flopbucket wrote:
I know that .* can not be overloaded but I am looking for some
background information for this. I have no need or desire to overload
it, but I am just curious from an educational point of view.

And what does this exactly refer to?
It's called "member access through a pointer to member" (or some such).

struct foo {
int a;
int b;
};

int main() {
foo f;
int foo::*pmem = &foo::a;
f.*pmem = 42; // same effect as f.a = 42;
pmem = &foo::b;
f.*pmem = 42; // same effect as f.b = 42;
}
Is it .* as in:

struct foo
{
int *bar;
}

int val = foo.*bar;
That should be a syntax error.
Or is there some other case when .* is used? Curious as to the
reasons why it can not be overloaded. I did not realize that .* is
an operator itself, I always thought of . * as two different
operators (i.e. as in the the above example).


What book are you reading that doesn't explain pointers to members?

As to *why* certain things the way they are, read "Design and Evolution
of C++" by Stroustrup, plenty of things are explained there. I bet you
could also find answers to "why some operators cannot be overloaded" on
the web.

V
--
Please remove capital As from my address when replying by mail
May 10 '06 #2
flopbucket wrote:
Hi,

I know that .* can not be overloaded but I am looking for some
background information for this. I have no need or desire to overload
it, but I am just curious from an educational point of view.

And what does this exactly refer to? Is it .* as in:

struct foo
{
int *bar;
}

int val = foo.*bar;
No. That would have to be

int val = *foo.bar;
Or is there some other case when .* is used?


It is for pointers to members, like:
struct foo
{
int bar;
};

int foo::* val = &foo::bar;

foo f;
f.*val = 3;

This is rarely used with pointers to member variables, because you can also
use a normal pointer, but for member functions's it's different. You can't
let a normal function pointer point to a non-static member function, so you
have to use a pointer to member for it.

May 10 '06 #3
> It's called "member access through a pointer to member" (or some such).

struct foo {
int a;
int b;
};

int main() {
foo f;
int foo::*pmem = &foo::a;
f.*pmem = 42; // same effect as f.a = 42;
pmem = &foo::b;
f.*pmem = 42; // same effect as f.b = 42;
}
OK, makes sense now. I was not familiar with this use before.
Is it .* as in:

struct foo
{
int *bar;
}

int val = foo.*bar;
That should be a syntax error.


Yes, I thought it looked a bit funny when I wrote it. Have been away
from C++ for a while.

As to *why* certain things the way they are, read "Design and Evolution
of C++" by Stroustrup, plenty of things are explained there. I bet you
could also find answers to "why some operators cannot be overloaded" on
the web.


Have a copy of that, but have not read it in several years. Will dig
it out and read through it again.

Thanks for the response.

May 10 '06 #4
Rolf Magnus wrote:

This is rarely used with pointers to member variables, because you can also
use a normal pointer,


A pointer to member data points to the data for any object that you
apply it to. An ordinary pointer points to the data member in the object
that you pointed it to. For example,

struct S
{
int a;
};

int S::*mptr = &S::a;

S s1;
s1.*mptr = 3; // assigns to s1.a
S s2;
s2.*mptr = 4; // assigns to s2.a

int *data_ptr = &s1.a;
*data_ptr = 5; // asigns to s1.a
// can't use data_ptr to point to s2.a without reassigning its value:

data_ptr = &s2.a;
*data_ptr = 6; // assigns to s2.a

--

Pete Becker
Roundhouse Consulting, Ltd.
May 10 '06 #5
>A pointer to member data points to the data for any object that you
apply it to. An ordinary pointer points to the data member in the object
that you pointed it to. For example,

[snip]


Thanks for this, makes sense. After being away from C++ for a while
lots of little details like this have slipped my mind.

May 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by joesoap | last post: by
reply views Thread by Martin Magnusson | last post: by
3 posts views Thread by Sensei | last post: by
14 posts views Thread by lutorm | last post: by
6 posts views Thread by YUY0x7 | last post: by
3 posts views Thread by y-man | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.