const结构&成员指针 [英] const structure & member pointers

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

问题描述




给出一个指针结构:

struct Example_Struct

{

unsigned char * ptr_buffer;

unsigned int * ptr_numbers;

};


一个接受结构的函数:

void My_Function(struct Example_Struct * es)

{

es-> ptr_buffer [0] = 0;

es-> ptr_numbers [2] = 7;

返回;

}


上述函数是否修改了{

}结构的实例?


指向结构的指针可以作为指向const数据的
传递吗? />

-----------在回复之前剪断这里-------

背景

- ----------

在我的程序中,我有一个结构,包含

指向消息,消息数据和
$ b $的指针b响应缓冲区。消息的内容,

数据和响应缓冲区将被更改;但

他们的位置不会。换句话说,

我正在更改指针指向的数据

但不是指针的值。


PC -Lint说我可以将结构声明为

指向const数据的指针。


------------

Crossposting

------------

这是交叉新闻:comp.lang.c,

news:comp.lang.c ++和news:alt.comp.learn.lang.c-c ++

因为它处理共同的基本问题
$ b使用C和C ++语言$ b,并且可能对所有组中的读者感兴趣。


-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题:< a rel =nofollowhref = http://www.eskimo.com/~scs/c-faq/top.htmltarget =_ blank> http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍

Hi,

Given a structure of pointers:
struct Example_Struct
{
unsigned char * ptr_buffer;
unsigned int * ptr_numbers;
};

And a function that will accept the structure:
void My_Function(struct Example_Struct * es)
{
es->ptr_buffer[0] = 0;
es->ptr_numbers[2] = 7;
return;
}

Does the above function modify the {instance of
the} structure?

Can the pointer to the structure be passed as
pointing to const data?

----------- Snip here before replying -------
Background
-----------
In my program I have a structure containing
pointers to messages, message data, and a
response buffer. The content of the message,
data and response buffer will be changed; but
their locations will not be. In other words,
I am changing the data the pointers point to
but not the value of the pointers.

PC-Lint says I can declare the pointer to
the structure as a pointer to const data.

------------
Crossposting
------------
This is crossposted to news:comp.lang.c,
news:comp.lang.c++ and news:alt.comp.learn.lang.c-c++
since it deals with a fundamental issue shared
by both the C and C++ language and may be of
interest to readers in all groups.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

推荐答案



" Thomas Matthews" <钍**************************** @ sbcglobal.net>写在

消息新闻:2k *************** @ newssvr15.news.prodigy.com。 ..

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:2k***************@newssvr15.news.prodigy.com. ..


给定一个指针结构:
struct Example_Struct
{
unsigned char * ptr_buffer;
unsigned int * ptr_numbers;
};

一个接受结构的函数:
void My_Function(struct Example_Struct * es)

es - > ptr_buffer [0] = 0;
es-> ptr_numbers [2] = 7;
返回;
}

以上函数是否修改了{结构的实例?

指向结构的指针是否可以作为
指向const数据传递?
Hi,

Given a structure of pointers:
struct Example_Struct
{
unsigned char * ptr_buffer;
unsigned int * ptr_numbers;
};

And a function that will accept the structure:
void My_Function(struct Example_Struct * es)
{
es->ptr_buffer[0] = 0;
es->ptr_numbers[2] = 7;
return;
}

Does the above function modify the {instance of
the} structure?

Can the pointer to the structure be passed as
pointing to const data?




是的,但只是因为struct包含指针。如果结构包含

数组,答案是否定的。


这部分是特定于C ++的。这是一个很好的例子,为什么公共数据成员

几乎总是一个坏主意。如果ptr_buffer和ptr_numbers成员

哪里不公开,那么你可以控制对它们的访问,并通过包含或

省略来自访问ptr_buffer和
的成员函数的const
ptr_numbers你可以控制什么是

Example_Struct结构的修改。对于公共数据,你没有这样的控制权。


john



Yes, but only because the struct contains pointers. If the struct contained
arrays the answer is no.

This part is C++ specific. This is a good example of why public data members
are almost always a bad idea. If the ptr_buffer and ptr_numbers members
where not public then you could control access to them and by including or
omitting const from the member functions that access ptr_buffer and
ptr_numbers you could control what consistuted a modification of the
Example_Struct struct. With public data you have no such control.

john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:c5 ************ @ ID-196037.news.uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5************@ID-196037.news.uni-berlin.de...
< 托马斯·马修斯(Thomas Matthews) <钍**************************** @ sbcglobal.net>在消息新闻中写道:2k *************** @ newssvr15.news.prodigy.com。 ..

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:2k***************@newssvr15.news.prodigy.com. ..


给定一个指针结构:
struct Example_Struct
{
unsigned char * ptr_buffer;
unsigned int * ptr_numbers;
};

一个接受结构的函数:
void My_Function(struct Example_Struct * es)


没有理由使用struct这个词。在上面的参数列表中。 (BTW,

将它放在那里是合法的吗?)

{
es-> ptr_buffer [0] = 0;
es- > ptr_numbers [2] = 7;
返回;
}

上面的函数是否修改了{of
}结构?

指向结构的指针可以作为
指向const数据传递吗?
是的,但只是因为struct包含指针。如果结构
Hi,

Given a structure of pointers:
struct Example_Struct
{
unsigned char * ptr_buffer;
unsigned int * ptr_numbers;
};

And a function that will accept the structure:
void My_Function(struct Example_Struct * es)
There''s no reason for the word "struct" in the parameter list above. (BTW,
is it legal to have it there?)
{
es->ptr_buffer[0] = 0;
es->ptr_numbers[2] = 7;
return;
}

Does the above function modify the {instance of
the} structure?

Can the pointer to the structure be passed as
pointing to const data?
Yes, but only because the struct contains pointers. If the struct



包含数组,则答案为否。


contained arrays the answer is no.




如果成员是数组,为什么不是这种情况而不是指针?

该函数接受一个指针(按值),因此它指向同一个结构

对象(不是它的副本)作为它的任何地方是的,从吗?

由于它是相同的结构而不是副本,该结构的成员是相同的成员,无论他们是否是数组,字符串,指针,

或其他什么。我在这里错过了什么吗?


-Howard



Why would it not be the case if the members were arrays instead of pointers?
The function takes a pointer (by value), so it points to the same struct
object (not a copy of it) as the one wherever it is called from, right?
Since it is the same struct and not a copy, the members of that struct are
the same members, regardless of whether they''re arrays, strings, pointers,
or whatever. Am I missing something here?

-Howard


Howard写道:
Howard wrote:
" Thomas Matthews" <钍**************************** @ sbcglobal.net>在消息新闻中写道:2k *************** @ newssvr15.news.prodigy.com。 ..
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:2k***************@newssvr15.news.prodigy.com. ..
以及一个接受结构的函数:
void My_Function(struct Example_Struct * es)
And a function that will accept the structure:
void My_Function(struct Example_Struct * es)



结构这个词没有理由。在上面的参数列表中。 (BTW,
将它放在那里是合法的吗?)


There''s no reason for the word "struct" in the parameter list above. (BTW,
is it legal to have it there?)




在C中它是非法的没有它。所以是的,有足够的理由让它在那里。为什么这个被广泛交叉我

不知道。


-

托马斯。



In C it is "illegal" to not have it there. So yes, there is plenty
of reason to have it there. Why this is crossposted so widely I
don''t know though.

--
Thomas.


这篇关于const结构&amp;成员指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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