The difference between class and struct in Swift

Source: Internet
Author: User
Classes (Class) and struct (struct) differences

1. Memory management method is different, class reference type, allocated on the heap. struct value type, allocated on the stack.

2. Class, there is a destructor. The structure cannot have a destructor, and the playground is tested directly to die.

3. Struct constructor, the constructor with parameters is automatically generated. The class does not generate constructors with parameters for properties that have an initialization assignment.

2. Classes have inheritance attributes, structs do not inherit attributes, and there is no natural overload of member properties and member methods, class properties, and class methods.

Because the struct has no inheritance, the operator function needs static modification and cannot be modified by class;

struct:static func + (left:a,right:a), A {...}
class:static func + (left:a,right:a), A {...} or
Final class Func + (LEFT:A,RIGHT:A), A {...}

same Point 1, other advanced features are similar, the structure and class have the characteristics:

operator function, paradigm, protocol adoption, observer

The difference between class and struct in Swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.