这个问题已经在c# /. net上下文中提出过了。

现在我想学习c++中结构体和类的区别。请讨论在OO设计中选择一种或另一种的技术差异以及原因。

我将从一个明显的区别开始:

如果没有指定public:或private:,结构体的成员默认为public;默认情况下,类的成员是私有的。

我敢肯定,在c++规范的晦涩角落里还会发现其他不同之处。


当前回答

根据c++编程语言中的Stroustrup:

你使用哪种风格取决于环境和品味。对于所有数据都是公共的类,我通常更喜欢使用struct。我认为这样的类“不是很合适的类型,只是数据结构”。

在功能上,除了public / private没有区别

其他回答

struct和class之间的主要区别是,在struct中只能声明不同数据类型的数据变量,而在class中可以声明数据变量和成员函数,因此可以通过函数操作数据变量。

-> another handy thing that i find in class vs struct is that while implementing files in a program if you want to make some operations of a struct again and again on every new set of operations you need to make a separate function and you need to pass object of struct after reading it from the file so as to make some operations on it . while in class if you make a function that does some operations on the data needed everytime..its easy you just have to read object from file and call the function..

但这取决于程序员认为哪种方式合适……对我来说,我更喜欢类,因为它支持oop,这就是它几乎在所有语言中实现的原因,它是所有编程的美妙特性;-)

是的,我忘记提到的最令人难忘的区别是类支持数据隐藏,也支持在内置数据类型上执行的操作,而struct不支持!

. In classes all the members by default are private but in structure members are public by default. There is no term like constructor and destructor for structs, but for class compiler creates default if you don't provide. Sizeof empty structure is 0 Bytes wer as Sizeof empty class is 1 Byte The struct default access type is public. A struct should typically be used for grouping data. The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data. In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance. In C++ structures and classes are passed by value, unless explicitly de-referenced. In other languages classes and structures may have distinct semantics - ie. objects (instances of classes) may be passed by reference and structures may be passed by value. Note: There are comments associated with this question. See the discussion page to add to the conversation.

还有一个不成文的规则告诉我们: 如果类的数据成员与自身没有关联,则使用struct。 如果数据成员的值依赖于另一个数据成员的值,则使用class。

f.e

class Time
{
    int minutes;
    int seconds;
}

struct Sizes
{
    int length;
    int width;
};

类和结构之间的区别是关键字之间的区别,而不是数据类型之间的区别。这两个

struct foo : foo_base { int x;};
class bar : bar_base { int x; };

两者都定义了一个类类型。这里关键字的区别在于默认访问权限的不同:

Foo::x是公共的,foo_base是公共继承的 Bar::x是私有的,bar_base是私有继承的

STRUCT是一种抽象数据类型,它根据结构规范划分给定的内存块。结构体在文件序列化/反序列化中特别有用,因为结构体通常可以被逐字写入文件。(例如,获取一个指向结构的指针,使用SIZE宏计算要复制的字节数,然后将数据移进或移出结构。)

类是一种不同类型的抽象数据类型,试图确保信息隐藏。在内部,可以有各种各样的诡计,方法,临时变量,状态变量。等等,它们都被用来为任何希望使用该类的代码提供一致的API。

实际上,结构体是关于数据的,类是关于代码的。

但是,您确实需要理解这些仅仅是抽象。创建看起来很像类的结构体和看起来很像结构体的类是完全可能的。事实上,最早的c++编译器只是将c++代码转换为C的预编译器。因此,这些抽象有助于逻辑思维,而不一定是计算机本身的资产。

Beyond the fact that each is a different type of abstraction, Classes provide solutions to the C code naming puzzle. Since you can't have more than one function exposed with the same name, developers used to follow a pattern of _(). e.g. mathlibextreme_max(). By grouping APIs into classes, similar functions (here we call them "methods") can be grouped together and protected from the naming of methods in other classes. This allows the programmer to organize his code better and increase code reuse. In theory, at least.