Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage preference between a struct and a class in D language

Tags:

d

When should I define a type as a struct or as a class?

I know that struct are value types while classes are reference types. So I wonder, for example, should I define a stack as a struct or a class?

like image 604
Fred Avatar asked Jun 10 '12 00:06

Fred


People also ask

What is the difference between a struct and a class?

Difference between Structs and Classes: Struct are value types whereas Classes are reference types. Structs are stored on the stack whereas Classes are stored on the heap. Value types hold their value in memory where they are declared, but a reference type holds a reference to an object in memory.

When should struct be used instead of class?

Deciding whether to use a struct or a class to implement a given type mostly comes down to the decision of whether we want that new type to have value or reference semantics. Class instances each have an identity and are passed by reference, while structs are handled and mutated as values.

Why do we use class instead of structure?

The major difference like class provides the flexibility of combining data and methods (functions ) and it provides the re-usability called inheritance. Struct should typically be used for grouping data. The technical difference comes down to subtle issues about default visibility of members.


1 Answers

Reason #1 to choose struct vs class: classes have inheritance, structs do not. If you need polymorphism, you must use classes.

Reason #2: structs are normally value types (though you can make them reference types if you work at it). Classes are always reference types. So, if you want a value type, choose a struct. If you want a reference type, it's easiest to go with a class.

Reason #3: If you have a type with a lot of data members, then you're probably going to want a reference type (to avoid expensive copying), in which case, you're probably going to choose a class.

Reason #4: If you want deterministic destruction of your type, then it's going to need to be a struct on the stack. Nothing on the GC heap has deterministic destruction, and the destructiors/finalizers of stuff on the GC heap may never be run. If they're collected by the GC, then their finalizers will be run, but otherwise, they won't. So, if you want your type to automatically be destroyed when it leaves scope, you need to use a struct and put it on the stack.

As for your particular case, containers should normally be reference types (copying all of their elements every time that you pass one around would be insanely expensive), and a Stack is a container, so you're going to want to use a class unless you want to go to the trouble of making it a ref-counted struct, which is decidedly more work. It just has the advantage of guaranteeing that its destructor will run when it's not used anymore.

On a side note, if you create a container which is a class, you're probably going to want to make it final so that its various functions can be inlined (and won't be virtual if that class doesn't derive from anything other than Object and they're not functions that Object has), which can be important for something like a container where performance can definitely matter.

like image 65
Jonathan M Davis Avatar answered Sep 26 '22 23:09

Jonathan M Davis