473,766 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to class data member question

(I posted this topic yesterday, but it disappeared. I don't know why,
so I re-post it here)
Hello. I am rather confused about class data member pointers. Take the
following class as an example:
class MyClass
{
public:
int n;
};

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

But at the same time, I can also just use a common data pointer:
MyClass my;
int *pn = &my.n;
*pn = 123;

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

Sep 18 '07 #1
2 2415
WaterWalk wrote:
(I posted this topic yesterday, but it disappeared. I don't know why,
so I re-post it here)
Hello. I am rather confused about class data member pointers. Take the
following class as an example:
class MyClass
{
public:
int n;
};

I can use the data member pointer syntax to access MyClass::n:
typedef int MyClass*pn_t;
typedef int MyClass::*pn_t; // you seem to have lost the colons
pn_t pn = &MyClass::n;
MyClass my;
pn = &my.n;
*pn = 123;
This is not a valid syntax. You need to specify the instance of the
class _explicitly_, even if it's a 'this':

this->*pn = 123;

or, in your case it MUST be

my.*pn = 123;

You may be using some kind of compiler extension. If that's the case,
make sure to disable compiler extensions during learning.
But at the same time, I can also just use a common data pointer:
MyClass my;
int *pn = &my.n;
*pn = 123;

I don't know if the second method is legal in the c++ standard. If it
is, why bother to have the first method?
Pointers to members have use when you need to manipulate a certain
member of the class but the instance is not known until some time later.
In your case, 'my' is the instance, and it's known before you even take
the address of 'n'. What if you have to decide later? This example is
somewhat contrived, but still

struct Foo
{
int a, b;
};

void set_member(Foo& foo, int Foo::*which_mem ber)
{
foo.*which_memb er = 42;
}

int main(int argc, char *argv[])
{
Foo f1, f2;
if (argc & 1 == 0) // 2, 4, etc.
{
if (*argv[1] == 'a')
set_member(f1, &Foo::a);
if (*argv[1] == 'b')
set_member(f1, &Foo::b);
}
else
{
if (argv[0][1] == 'u')
set_member(f2, &Foo::a);
if (argv[0][1] == 's')
set_member(f2, &Foo::b);
}
}

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, 11:22 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
WaterWalk wrote:
(I posted this topic yesterday, but it disappeared. I don't know why,
so I re-post it here)
Hello. I am rather confused about class data member pointers. Take the
following class as an example:
class MyClass
{
public:
int n;
};
I can use the data member pointer syntax to access MyClass::n:
typedef int MyClass*pn_t;

typedef int MyClass::*pn_t; // you seem to have lost the colons
pn_t pn = &MyClass::n;
MyClass my;
pn = &my.n;
*pn = 123;

This is not a valid syntax. You need to specify the instance of the
class _explicitly_, even if it's a 'this':

this->*pn = 123;

or, in your case it MUST be

my.*pn = 123;

You may be using some kind of compiler extension. If that's the case,
make sure to disable compiler extensions during learning.
Sorry, I made a mistake. Thanks for your correcting.

Sep 18 '07 #3

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

Similar topics

5
9375
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
4
4739
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
6
8828
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
6
2295
by: Itay_k | last post by:
Hello, I want a member in my class that will save pointer to pointer to System::Drawing::Image class. When I write on my class code: System::Drawing::Image **bmp; I get this error message: error C3160: 'bmp' : cannot declare interior __gc pointer or reference as a
22
2778
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
5
2269
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
12
540
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass { public: int n; }; The pointer to MyClass.n shall be defined like this:
5
4663
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
31
1984
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
5
3651
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8835
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.