473,402 Members | 2,046 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,402 software developers and data experts.

Problem with pointer-to-class-data-member

(Sorry if I duplicated this post. My previous post somehow
disappeared.)

Hello. I am rather confused about pointers to class data members. Take
the following class as an example:
class MyClass
{
public:
int n;
};

I can use the pointer-to-member syntax to access MyClass::n:
typedef int MyClass*pn_t;
pn_t pn = &MyClass::n;
MyClass my;
my.*pn = 123;

But at the same time, the common pointer-to-data also seems to work:
MyClass my;
int *pn = &my.n;
*pn = 123;

I don't know if the second method is legal in c++ standard. If it is,
why bother to have the first method?

Sep 18 '07 #1
2 1318
WaterWalk wrote:
(Sorry if I duplicated this post. My previous post somehow
disappeared.)
[..]
No, it didn't. I just replied to it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #2
On Sep 18, 2:52 pm, WaterWalk <toolmas...@163.comwrote:
>
I can use the pointer-to-member syntax to access MyClass::n:
typedef int MyClass*pn_t;
This is a syntax error. I think you meant:
typedef int MyClass::* pn_t;

although I never use that syntax so I could be wrong.
Also, pointer typedefs are confusing, so I suggest not
using them until you grok the subject matter.
pn_t pn = &MyClass::n;
MyClass my;
my.*pn = 123;

But at the same time, the common pointer-to-data also seems to work:
MyClass my;
int *pn = &my.n;
*pn = 123;

I don't know if the second method is legal in c++ standard.
Yes
If it is, why bother to have the first method?
Well, they do different things. The first pointer is
not bound to any particular class instance. In the
first one you could write:
MyClass mz;
mz.*pn = 456;

and there is no such equivalent for the second version.

Sep 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Davy | last post by:
I found a interesting pointer problem. What does the pointer mean? EXTERN short (*blocks); And what does the malloc mean? blocks = (short (*))malloc(count*sizeof(short )); Any suggestions...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
8
by: Daniel Wild | last post by:
Hello, I have a problem with a macro which will return a pointer. In the header trolley_position_SL_GR is defined: #define trolley_position_SL_GR ((real_T*) ssGetPWorkValue(S,4)) The...
6
by: Ratan | last post by:
In my assigment,I have a question: "Between a long pointer and a char pointer,which one consumes more momery.Explain." But i have read that all pointer have same size as of 2 bytes.So what can...
26
by: David | last post by:
Hi all, I am really new in C++. I met a problem. Could someone help me? I have a base class: class a { protected: int b; public:
27
by: onkar | last post by:
#include<stdio.h> int main(void){ char a="abcde"; char *p=a; p++; p++; p='z'; printf("%s\n",p); return 0; }
3
by: josh | last post by:
Why if I use: Array *Array::operator=( const Array *right ) { if ( right != this ) { // check for self-assignment // for arrays of different sizes, deallocate original // left side array,...
4
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, As the Subject suggests, array variable will retrogress as the parameter of the function actually taking pointer as its argument, like this: int f(int* i) { cout << sizeof(i) <<...
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
49
by: Bert | last post by:
Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1 Why doesn't this work? /* ID: albert.4 LANG: C TASK: gift1 */
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.