Was reading Wikipedia for RAII when just saw Single
and Shared
ownership.
Googled for it and couldn't find any useful answer!
Could some one possibly explain this concept for a schoolboy?
Pros of Shared OwnershipDeposits are generally lower than buying on the open market. Shared Ownership makes mortgages more accessible, even if you're on a lower wage. Your monthly repayments can often work out cheaper than if you had an outright mortgage.
Shared Ownership is a type of affordable home ownership when a purchaser takes out a mortgage on a share of a property and pays rent to a landlord on the remaining share. For example, someone might buy a 50% share in a property, and pay rent to the landlord on the remaining 50%.
Shared ownership involves buying a share of a property and paying rent on the rest. Shared equity involves paying a low property deposit, using an equity loan for a percentage of the property's value, and getting a mortgage for the remaining amount.
It is essentially unique_ptr
vs shared_ptr
.
Single ownership, otherwise known as unique ownership, means that the resource is owned by a single class instance. Once that instance ceases to exist the resource is released (via the destructor). The majority of RAII classes you find have unique ownership, such as std::vector
.
Shared ownership means the resource is shared between multiple class instances. The resource is only released once every instance ceases to exist and thus requires some form of reference counting or garbage collection. An example of where you would want shared ownership is a handle to a very expensive to copy immutable resource. I've seen it used in graphs too.
It might help to think in terms of pointers. Single ownership will only have 1 owning pointer, shared will have multiple. Of course, RAII might not involve pointers.
+---------------+
|Shared instance|
+--------+ +------------+--+ +---------------+
|Resource| | +----------+Shared instance|
+--------+ v v +---------------+
^ +--------+
| |Resource|<-----------+
| +--------+ +---+-----------+
| ^ |Shared instance|
+------+--------+ | +---------------+
|Unique Instance| |
+---------------+ |
+------+--------+
|Shared instance|
+---------------+
Ownership is strongly tied with the concept of variable lifetimes.
If you can answer the question, when is it ok for this chunk of memory to go away?, then you can answer the issue of ownership.
If that chunk of memory is only tied with the lifetime of one variable, then you have single ownership.
Note that it's also important to think about heap or dynamic allocation vs stack or automatic variables. With automatic variables, when they go out of scope, the memory associated with them are reaped. With dynamic allocation, the same is not true, in the general case. If you use new facilities like std::unique_ptr<> for single ownership, then you can make a chunk of dynamic memory clean itself up when it falls out of scope. If there are many references to that chunk of memory with uncertain ordering of when the references will go away, then you may need something like a std::shared_ptr<> for multiple ownership.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With