c++代码:类或结构:难以理解

发布于 2024-12-02 23:32:13 字数 790 浏览 0 评论 0原文

可能的重复:
C++中struct和class有什么区别

我正在查看某个结构的头文件,但看起来函数是该结构的一部分......那么这是一个类,但如何放弃?或使用?

struct Recording
{
    FLAG mode;  
    unsigned short intervals;   
      unsigned short saved_cycles;                                      
    virtual void SavetoFile( FILE *file, 
        bool Control, 
        PhaseData *__phaseData = NULL   
        );

    virtual bool LoadfromFile( FILE *file, 
        bool Control, 
        PhaseData *__phaseData = NULL 
        );
};

Possible Duplicate:
What are the differences between struct and class in C++

I am looking at somebody header file for a structure, but it looks like functions are a part of this structure... so then this is a class but how to abstantiate? or use?

struct Recording
{
    FLAG mode;  
    unsigned short intervals;   
      unsigned short saved_cycles;                                      
    virtual void SavetoFile( FILE *file, 
        bool Control, 
        PhaseData *__phaseData = NULL   
        );

    virtual bool LoadfromFile( FILE *file, 
        bool Control, 
        PhaseData *__phaseData = NULL 
        );
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉溺在你眼里的海 2024-12-09 23:32:13

在 C++ 中,classstruct 是相同的,只是前者的默认访问说明符是 private,而后者的默认访问说明符是 公开

class Base { /* .. */ };

class Foo : Base
{
  int i;
};

struct Bar : Base
{
  int i;
};

在上面的代码中,Foo 私有地继承自 Base,而 Bar 则公开继承。同样,Foo::i 是私有的,而 Bar::i 是公共的。请注意,在这两种情况下,i 的可见性与继承无关,即即使 FooBar 没有,它也是相同的。从Base继承。

除了这些差异之外,你可以用一个做的所有事情,你也可以用另一个做。

In C++ class and structare identical, except that the default access specifier for the former is private, while for the latter it is public.

class Base { /* .. */ };

class Foo : Base
{
  int i;
};

struct Bar : Base
{
  int i;
};

In the above code, Foo inherits privately from Base while Bar does so publicly. Similarly, Foo::i is private while Bar::i is public. Note that the visibility of i in both cases has nothing to do with inheritance i.e. it'd be the same even if Foo and Bar did not inherit from Base.

Other than these differences, everything that you can do with one, you can also do with the other.

甚是思念 2024-12-09 23:32:13

在 C++ 中,classstruct 之间唯一有意义的区别是,class 的变量和函数默认是私有的,而 class 的变量和函数默认是私有的。 code>struct 它们默认是公共的。

In C++ the only meaningful difference between a class and a struct is that variables and functions of a class are private by default, and of a struct they are public by default.

晨光如昨 2024-12-09 23:32:13

使用 class 关键字时,所有子对象(成员和基类)默认都是私有的,使用 struct 时默认是公共的,并且都定义了类类型。

All subobjects (members AND bases) are private by default when using the class keyword, public by default with struct, and both define a class type.

凶凌 2024-12-09 23:32:13

结构的特点是:

所有成员默认都是公共的。
默认情况下继承是公共的,即如果一个结构继承自类或结构,那么它就是公共的。

对于一个类,上述两个默认情况下都是私有的,即如果您不指定其他内容。

关于数据成员,这使得它与 C 兼容(其中结构只能有数据成员),这意味着您也可以在 C++ 中使用 C 结构。

有时,即使在 C++ 中,仍然使用结构,但我自己的偏好通常是使用“类”一词,除非我特别希望所有数据成员公开,这种情况发生在您只是将它们分组在一起的情况下。

发布的代码可能会受益于私有数据成员以及常量正确性,并可能使虚拟方法抽象。还需要一个虚拟析构函数,因为它有虚拟方法。

此外,除非您正在实现“系统”,否则不应使用以两个下划线开头的标识符。

The features are struct are that:

All members are public by default.
Inheritance is public by default, i.e. if a struct inherits from a class or a struct it is public.

With a class both of the above are private by default, i.e. if you oo not specify others.

With regards to data members, this makes it compatible with C (where structs can only have data members) which means you can use your C structs in C++ too.

There are occasions where, even in C++, structs are still used, but my own preference is generally to use the word "class" unless I specifically want all my data members public, which happens on occasion where you are simply grouping them together.

The code posted would probably benefit from private data members, as well as const-correctness and possibly making the virtual methods abstract. Also would need a virtual destructor as it has virtual methods.

In addition you should not be using identifiers beginning with two underscores unless you are implementing "the system".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文