类与结构? [英] Classes vs Structs?

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

问题描述

Google上有很多关于此主题的信息。但在Bjarne Stroustrup

的书页225中,他写道:


前一小节中的日期声明(声明为

struct)提供了一组用于操作Date的函数。但是,

没有指定那些函数应该是唯一直接依赖于Date?s表示的
和唯一直接访问的函数

类Date的对象。这个限制

可以通过使用类而不是结构来表示

但是我不明白为什么你需要一个类,第234页他甚至显示

您可以在结构中拥有私有和公共字段和函数

。他写的这本书中是否有错误,你需要一个班级才能使数据私密化?

There is a lot of info on this topic on google. But in Bjarne Stroustrup
''s book page 225 he writes:

"The declaration of Date in the previous subsection (declared as a
struct) provides a set of functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ?s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don''t see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?

推荐答案

桌面写道:
desktop wrote:

谷歌上有很多关于这个主题的信息。但在Bjarne Stroustrup

的书页225中,他写道:


前一小节中的日期声明(声明为

struct)提供了一组用于操作Date的函数。但是,

没有指定那些函数应该是唯一直接依赖于Date?s表示的
和唯一直接访问的函数

类Date的对象。这个限制

可以通过使用类而不是结构来表示


但是我不明白为什么你需要一个类,第234页他甚至显示了

你可以在一个结构中拥有私有和公共字段和函数

。书中是否有错误,他写道你需要一个班级才能使数据私有化?
There is a lot of info on this topic on google. But in Bjarne Stroustrup
''s book page 225 he writes:

"The declaration of Date in the previous subsection (declared as a
struct) provides a set of functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ?s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don''t see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?



不,这不是错误。 Stroustrup并没有说你需要创造一个班级的b $ b。他说你可以解决创建课程的问题。

实际上,你可以解决它,并在

结构中声明成员私有。无论如何,它已经被弃用了struct关键字。阅读
http ://www.parashift.com/c++-faq-lit...s.html#faq-7.8


问候,


Zeppe

no, it''s not an error. Stroustrup doesn''t say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It''s deprecated, anyway, such a use of the struct keyword. Read
http://www.parashift.com/c++-faq-lit...s.html#faq-7.8

Regards,

Zeppe


6月7日下午1:55,Zeppe

< zep_p @ .remove.all.this.long .comment.yahoo.itwrote:
On Jun 7, 1:55 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :

desktop写道:
desktop wrote:

这方面有很多信息关于谷歌的话题。但是在Bjarne Stroustrup

的书页225中,他写道:
There is a lot of info on this topic on google. But in Bjarne Stroustrup
''s book page 225 he writes:


之前的日期声明subsection(声明为

struct)提供了一组用于操作Date的函数。但是,

并没有指定那些函数应该是唯一直接依赖于Date表示的函数和唯一直接访问的函数

类Date的对象。这个限制

可以通过使用类而不是结构来表示。
"The declaration of Date in the previous subsection (declared as a
struct) provides a set of functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ''s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"


但我不明白为什么你需要上课,第234页他甚至表示

你可以在一个结构中拥有私有和公共字段和函数

。书中是否有错误,他写道你需要一个班级才能使数据私有化?
But I don''t see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?



不,这不是错误。 Stroustrup并没有说你需要创造一个班级的b $ b。他说你可以解决创建课程的问题。

实际上,你可以解决它,并在

结构中声明成员私有。无论如何,它已经被弃用了struct关键字。 Readhttp://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8


问候,


Zeppe


no, it''s not an error. Stroustrup doesn''t say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It''s deprecated, anyway, such a use of the struct keyword. Readhttp://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8

Regards,

Zeppe



编码时你必须自己决定。

现在如果你想让你编码C ++但你想使用struct

,它取决于struct的限制。如果有什么东西

结构不能处理,那么你必须使用类。


阅读: http://carcino.gen.nz/tech/cpp/struct_vs_class.php


问候,




有些事情已知且事情未知,介于两者之间。

There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can''t handle then you got to use class.

Read this : http://carcino.gen.nz/tech/cpp/struct_vs_class.php

Regards,
ar

"There are things known and things unknown, in between are The Doors."


6月7日,11:05,Anonymous< ashwani.ra ... @ gmail.comwrote:
On 7 Jun, 11:05, Anonymous <ashwani.ra...@gmail.comwrote:

在编码时你必须自己决定某些事情。

现在如果你想用C ++编写代码但是你想使用struct

这将取决于结构的限制。如果有什么东西

struct无法处理,那么你必须使用类。
There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can''t handle then you got to use class.



您认为struct有什么限制?你怎么认为struct不能用b $ b来处理那个类呢?

What limits do you think struct has? What do you think struct can''t
handle that class can?


读这个: http://carcino.gen.nz/tech/cpp/struct_vs_class.php



该链接描述了结构和类的近似等价(尽管

在谈到成员时应该谈论成员和基类 -

见FAQ 7.8)并没有提到任何限制。或任何struct can''t

句柄。它似乎与你上面说的相矛盾。


Gavin Deane

That link describes the near-equivalence of struct and class (although
where it talks of members it should talk of members and base classes -
see FAQ 7.8) and doesn''t mention any "limit" or anything "struct can''t
handle". It appears to contradict what you say above.

Gavin Deane


这篇关于类与结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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