指向类数据成员问题的指针 [英] Pointer to class data member question

查看:103
本文介绍了指向类数据成员问题的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(昨天我发布了这个话题,但它消失了。我不知道为什么,

所以我在这里重新发布)

你好。我对类数据成员指针感到困惑。以下课程的

为例:

class MyClass

{

public:

int n;

};


我可以使用数据成员指针语法来访问MyClass :: n:

typedef int MyClass * pn_t;

pn_t pn =& MyClass :: n;

MyClass my;

pn =& my。 n;

* pn = 123;


但与此同时,我也可以使用通用数据指针:

MyClass我的;

int * pn =& my.n;

* pn = 123;


我不喜欢不知道第二种方法在c ++标准中是否合法。如果它是b $ b,为什么还要花第一种方法呢?

(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?

推荐答案

WaterWalk写道:
WaterWalk wrote:

(昨天我发布了这个话题,但它消失了。我不知道为什么,

所以我在这里重新发布)

你好。我对类数据成员指针感到困惑。以下课程的

为例:

class MyClass

{

public:

int n;

};


我可以使用数据成员指针语法来访问MyClass :: n:

typedef int MyClass * pn_t;
(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; //你似乎丢了冒号

typedef int MyClass::*pn_t; // you seem to have lost the colons


pn_t pn =& MyClass :: n;

MyClass my;

pn =& my.n;

* pn = 123;
pn_t pn = &MyClass::n;
MyClass my;
pn = &my.n;
*pn = 123;



这不是有效的语法。你需要指定

类_explicitly_的实例,即使它是''这个'':


this-> * pn = 123;


或者,在你的情况下它必须是


我的。* 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.


但与此同时,我也可以只需使用一个公共数据指针:

MyClass my;

int * pn =& my.n;

* pn = 123;


我不知道第二种方法在c ++标准中是否合法。如果它是
,为什么还要花第一种方法呢?
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?



指向成员的指针在你需要操作类的某个

成员时使用,但直到一段时间后才知道该实例。

在你的情况下,我的是实例,在你甚至把'b'的地址带到'n'之前就知道了。如果你以后必须决定怎么办?这个例子是有点人为的b $ b,但仍然是


struct Foo

{

int a, b;

};


void set_member(Foo& foo,int Foo :: * which_member)

{

foo。* which_member = 42;

}


int main(int argc,char * argv [])

{

Foo f1,f2;

if(argc& 1 == 0)// 2,4等。

{

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

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

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_member)
{
foo.*which_member = 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


9月18日上午11点22分,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Sep 18, 11:22 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

WaterWalk写道:
WaterWalk wrote:

(I昨天发布了这个话题,但它消失了。我不知道为什么,

所以我在这里重新发布)

你好。我对类数据成员指针感到困惑。以下课程的

为例:

class MyClass

{

public:

int n;

};
(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;
};


我可以使用数据成员指针语法来访问MyClass :: n:

typedef int MyClass * pn_t;
I can use the data member pointer syntax to access MyClass::n:
typedef int MyClass*pn_t;



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;
pn_t pn = &MyClass::n;
MyClass my;
pn = &my.n;
*pn = 123;



这不是有效的语法。你需要指定

类_explicitly_的实例,即使它是''这个'':


this-> * pn = 123;


或者,在你的情况下它必须是


我的。* 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.


这篇关于指向类数据成员问题的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆