Question:
what's the difference between structures and objects ?
eman_elmaged
2009-06-21 23:26:32 UTC
i still read an introduction to object and classes and i feel till now there's no difference between objects and structures they both have their own functions and data, they both use the dot operator?!
Three answers:
2009-06-25 22:26:23 UTC
Hm....



















A struct, like a union, groups variables into a single record.

The data within a class are called data members; the functions within a

class are called member functions.

Defferent memory use, a struct use same memory block. Unlike a class, a class use deffrent memory block for any data members and function members.

Ex, memory block for data members at address XXXX, but another member data in YYYY.
2009-06-22 06:33:03 UTC
Structures and classes differ in the following particulars:

Structures are value types; classes are reference types.

Structures use stack allocation; classes use heap allocation.

All structure members are Public by default; class variables and constants are Private by default, while other class members are Public by default. This behavior for class members provides compatibility with the Visual Basic 6.0 system of defaults.

A structure must have at least one nonshared variable or event member; a class can be completely empty.

Structure members cannot be declared as Protected; class members can.

A structure procedure can handle events only if it is a Shared Sub procedure, and only by means of the AddHandler statement; any class procedure can handle events, using either the Handles keyword or the AddHandler statement.

Structure variable declarations cannot specify initializers, the New keyword, or initial sizes for arrays; class variable declarations can.

Structures implicitly inherit from the ValueType class and cannot inherit from any other type; classes can inherit from any class or classes other than ValueType.

Structures are not inheritable; classes are.

Structures are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure; classes are terminated by the garbage collector, which calls Finalize on a class when it detects there are no active references remaining.

A structure does not require a constructor; a class does.

Structures can have nonshared constructors only if they take parameters; classes can have them with or without parameters.



Every structure has an implicit public constructor without parameters. This constructor initializes all the structure's data members to their default values. You cannot redefine this behavior.
Lie Ryan
2009-06-22 06:44:36 UTC
There are various subtle differences between struct and class, most of them language specific and even implementation specific, but the most important difference between the two is this:



"Structure is value type and Class is Reference type"



What that means is if you pass a structure as an argument to a function, or use a structure as return value of a function, the whole structure will be copied (unless you explicitly use its pointer).



OTOH when a class instance is passed as an argument or used as return value, its reference/pointer will be copied but not the class instance itself.



Because of the basic difference, the implication is there are other various subtler differences, however it is best not to assume a particular implementation's behavior would be the same as another implementation's behavior or worse that the behavior in a language would be the same or similar as in another language.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...