• 16993阅读
  • 7回复

【声明】关于C++中struct与class的区别的权威声明 [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2005-08-30
— 本帖被 XChinux 设置为精华(2010-08-04) —
我们既然讨论struct与class的区别,那就是只是说在C++中的struct与class。
下面的节选部分,是我从<<The C++ Programming Language 3rd Edition>>中截取出来的。关于struct与class的一节,请注意红色的部分,那是我们想要的,它的意思是说:
struct就是一个class,除了其默认成员上是public
在传统习惯上,我们使用struct来表示全部是public的数据成员的类型。
至于各位读者使用编译器编译后看到的结果什么的,那只能说明,各个编译器在进行优化的时候,进行了处理,但,在语法上,还是要按官方资料声明的为准。


10.2.8 Structures and Classes[class.struct]
By definition, a struct is a class in which members are by default public; that is,
    struct s{...

is simply shorthand for
    class s {public: ...



The access specifier private: can be used to say that the members following are private, just as public: says that the members following are public. Except for the different names, the following declarations are equivalent:
    class Data1 {
        int d, m, y;
    public:
        Data1(int dd, int mm, int yy);
        void add_year(int n);     // add n years
    };
    struct Data2 {
    private:
        int d, m, y;
    public:
        Data2(int dd, int mm, int yy);
        void add_year(int n);     // add n years
    };

Which style you use depends on circumstances and taste. I usually prefer to use struct for classes that have all data public. I think of such classes as "not quite proper types, just data structures." Constructors and access functions can be quite useful even for such structures, but as a shorthand rather than guarantors of properties of the type.
  It is not a requirement to declare data first in a class. In fact, it often makes sense to place data members last to emphasize the functions providing the public user interface. For example:
    class Data3{
    public:
        Data3(int dd, int mm, int yy);
        void add_year(int n);     // add n years
    private:
        int d, m, y;
    };

In real code, where both the public interface and the implementation details typically are more extensive than in tutorial examples, I usually prefer the style used for Data3.
  Access specifiers can be used many times in a single class declaration. for example:
    class Data4{
    public:
        Data4(int dd, int mm, int yy);
    private:
        int d, m, y;
    public:
        void add_year(int n);     //add n years
    };


Having more than once public section, as in Data4, tends to be messy. So does having more than one private section. However, allowing many access specifiers in a class is useful for machine-generated code.
[ 此贴被XChinux在2005-08-30 09:55重新编辑 ]
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线magicoy

只看该作者 1楼 发表于: 2005-08-30
程序容易理解
E文不太懂.
还是顶一下.
永 远 记 得 : 做 人 厚 道 一 点 好 。

离线enjoyo
只看该作者 2楼 发表于: 2005-08-30
先顶着

回头买本中文版的再看文字
最专业的软件技术社区:华竹技术论坛

http://sinoprise.com

http://sinoprise.net
离线acefunware

只看该作者 3楼 发表于: 2005-09-05
不是吧
想不到一个小问题 弄成这种样子
呵呵
email:acefunware@yahoo.com.cn
blog:http://acefunware.shineblog.com
离线tdrhsb
只看该作者 4楼 发表于: 2005-10-26
就是struct 默认所有的成员都是public而class都是private的啊,就没有其他区别了啊!
离线qiguoyin81

只看该作者 5楼 发表于: 2006-11-24
是的这就是他们的主要区别
离线sunyilong
只看该作者 6楼 发表于: 2008-07-24
顶一下
离线steinlee

只看该作者 7楼 发表于: 2010-03-04
struct remains in C++ because C++ is designed to be compatible to C which has struct.
In C++ struct is converted to class in compiling time.

It is interesting to look at the history of programming languages:
1. early Fortran has only arries which are used only for the same type of variables(like double, float, int).
2. struct was introduced in C in order to put different data types in one structure. For example:
      struct A
      {
          double a;
          int b;
          float c;
      }
3. functions were added to struct==>class in C++

Looks like small changes. However, they brought revolutions to software design.
[ 此帖被steinlee在2010-03-04 12:35重新编辑 ]
Looking for remote C/C++ and Qt 兼职
快速回复
限100 字节
 
上一个 下一个