CATEGORII DOCUMENTE |
DOCUMENTE SIMILARE |
||||||||
|
||||||||
The list of similarities between classes and structs is long-structs can implement interfaces, and can have the same kinds of members as classes. Structs differ from classes in several important ways, however: structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored either "on the stack" or "in-line". Careful programmers can sometimes enhance performance through judicious use of structs.
For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed by a program. The program below creates and initializes an array of 100 points. With Point implemented as a class, the program instantiates 101 separate objects-one for the array and one each for the 100 elements.
class
Point
}
class
Test
}
If Point is instead implemented as a struct, as in
struct
Point
}
then the test program instantiates just one object-the one for the array. The Point instances are allocated in-line within the array. This optimization can be misused. Using structs instead of classes can also make a program slower and fatter, as passing a struct instance as a value parameter causes a copy of the struct to be created. There is no substitute for careful data structure and algorithm design.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 923
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved