c++代码:类或结构:难以理解
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C++ 中,
class
和struct
是相同的,只是前者的默认访问说明符是private
,而后者的默认访问说明符是公开
。在上面的代码中,
Foo
私有地继承自Base
,而Bar
则公开继承。同样,Foo::i
是私有的,而Bar::i
是公共的。请注意,在这两种情况下,i
的可见性与继承无关,即即使Foo
和Bar
没有,它也是相同的。从Base
继承。除了这些差异之外,你可以用一个做的所有事情,你也可以用另一个做。
In C++
class
andstruct
are identical, except that the default access specifier for the former isprivate
, while for the latter it ispublic
.In the above code,
Foo
inherits privately fromBase
whileBar
does so publicly. Similarly,Foo::i
is private whileBar::i
is public. Note that the visibility ofi
in both cases has nothing to do with inheritance i.e. it'd be the same even ifFoo
andBar
did not inherit fromBase
.Other than these differences, everything that you can do with one, you can also do with the other.
在 C++ 中,
class
和struct
之间唯一有意义的区别是,class
的变量和函数默认是私有的,而class
的变量和函数默认是私有的。 code>struct 它们默认是公共的。In C++ the only meaningful difference between a
class
and astruct
is that variables and functions of aclass
are private by default, and of astruct
they are public by default.使用
class
关键字时,所有子对象(成员和基类)默认都是私有的,使用struct
时默认是公共的,并且都定义了类类型。All subobjects (members AND bases) are private by default when using the
class
keyword, public by default withstruct
, and both define a class type.结构的特点是:
所有成员默认都是公共的。
默认情况下继承是公共的,即如果一个结构继承自类或结构,那么它就是公共的。
对于一个类,上述两个默认情况下都是私有的,即如果您不指定其他内容。
关于数据成员,这使得它与 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".