这个问题已经在c# /. net上下文中提出过了。
现在我想学习c++中结构体和类的区别。请讨论在OO设计中选择一种或另一种的技术差异以及原因。
我将从一个明显的区别开始:
如果没有指定public:或private:,结构体的成员默认为public;默认情况下,类的成员是私有的。
我敢肯定,在c++规范的晦涩角落里还会发现其他不同之处。
这个问题已经在c# /. net上下文中提出过了。
现在我想学习c++中结构体和类的区别。请讨论在OO设计中选择一种或另一种的技术差异以及原因。
我将从一个明显的区别开始:
如果没有指定public:或private:,结构体的成员默认为public;默认情况下,类的成员是私有的。
我敢肯定,在c++规范的晦涩角落里还会发现其他不同之处。
当前回答
另一个主要区别是模板。据我所知,你可以在定义模板时使用类,但不能定义结构。
template<class T> // OK
template<struct T> // ERROR, struct not allowed here
其他回答
规范里没有,没有。主要的区别在于程序员在2年内阅读你的代码时的期望。结构体通常被假定为POD。当您为定义对象以外的目的定义类型时,也会在模板元编程中使用结构。
引用c++ FAQ,
[7.8] What's the difference between the keywords struct and class? The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults. Struct and class are otherwise functionally equivalent. OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.
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.
类是引用类型,结构是值类型。 我说类是引用类型, 基本上,它们将包含实例变量的地址。
例如:
Class MyClass
{
Public Int DataMember; //By default, accessibility of class data members
//will be private. So I am making it as Public which
//can be accessed outside of the class.
}
在主要方法中, 我可以使用new操作符创建该类的实例,为该类分配内存 并将其基址存储到MyClass类型变量(_myClassObject2)中。
Static Public void Main (string[] arg)
{
MyClass _myClassObject1 = new MyClass();
_myClassObject1.DataMember = 10;
MyClass _myClassObject2 = _myClassObject1;
_myClassObject2.DataMember=20;
}
在上面的程序中, MyClass _myClassObject2 = _myClassObject1; 指令指示两个MyClass类型的变量
myClassObject1 myClassObject2
并将指向相同的内存位置。 它基本上将相同的内存位置分配给另一个相同类型的变量。
如果我们在MyClass类型的任何一个对象中做的任何改变都会对另一个产生影响 因为两者都指向相同的内存位置。
“_myClassObject1。在这一行,对象的两个数据成员都包含10的值。 “_myClassObject2。DataMember = 20;”在这一行,对象的数据成员都包含值20。 最后,我们通过指针访问对象的数据成员。
与类不同,结构是值类型。 例如:
Structure MyStructure
{
Public Int DataMember; //By default, accessibility of Structure data
//members will be private. So I am making it as
//Public which can be accessed out side of the structure.
}
Static Public void Main (string[] arg)
{
MyStructure _myStructObject1 = new MyStructure();
_myStructObject1.DataMember = 10;
MyStructure _myStructObject2 = _myStructObject1;
_myStructObject2.DataMember = 20;
}
在上面的程序中, 使用new操作符和实例化MyStructure类型的对象 将地址存储到MyStructure类型的_myStructObject变量中 使用"_myStructObject1 "将值10赋给结构的数据成员。DataMember = 10”。
下一行, 我声明了另一个MyStructure类型的变量_myStructObject2,并将_myStructObject1赋值给它。 这里。net c#编译器创建了_myStructureObject1对象的另一个副本 将该内存位置分配给MyStructure变量_myStructObject2。
因此,无论我们对_myStructObject1做了什么改变,都不会对另一个MyStructrue类型的变量_myStructObject2产生影响。 这就是为什么我们说结构是值类型。
class的直接基类是Object而Structure的直接基类是继承自Object的ValueType。 类将支持继承,而结构则不支持。
我们怎么能这么说呢? 这背后的原因是什么? 答案是类。
它可以是抽象的、密封的、静态的和局部的,不能是私有的、受保护的和内部的。
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不支持!