473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1536
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.