ownership

General way to own a value (don't specify `Rc` or `Box`)

[亡魂溺海] 提交于 2019-12-04 04:14:18
问题 Is there a enum/trait for owned values in general, for when you don't want to specify how exactly the value is owned (either shared or not), but you just want to own it. I need to store references to closures in a struct, which means that they have to live as long as the struct lives. I can't copy them, of course, so they need to be references. But I don't want to make restrictions, so the user of the struct should be able to choose how they want to transfer the ownership. This is a general

What is the right way to expose resources owned by a class?

孤人 提交于 2019-12-04 00:02:07
Let's say I have a library which has a Document class. An instance of Document can own several instances of Field . Field has multiple subclasses (for example IntegerField and StringField ), and even the API user can subclass it and supply subclass instances to Document (let's say the user is allowed to develop a custom type of data to store in a field). I'd like to expose the Field instances owned by Document through an API in such a way that users can interact with them, but without transferring ownership . What is the right way to do this? I thought about: Exposing a const std::unique_ptr

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

戏子无情 提交于 2019-12-03 10:53:24
问题 What is the proper way of transferring ownership of a std::vector<unique_ptr<int> > to a class being constructed? Below is a code representation of what I want to do. I realize it is not correct (won't compile) and violates "uniqueness" whether I pass the vector to the constructor by value or by reference. I want Foo to be the new owner of the vector, and want the calling function to relinquish ownership. Do I need the constructor to take a std::unique_ptr<std::vector<std::unique_ptr<int> > >

single vs shared ownership meaning

夙愿已清 提交于 2019-12-03 07:22:36
问题 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? 回答1: 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,

Syntax guidelines for taking ownership and releasing objects in C++

喜欢而已 提交于 2019-12-03 07:13:37
I want to know - are there any guidelines about syntax of C++ (non-)member functions, that allows me to understand (without comments, if possible) the ownership policy of its arguments and return value. By ownership I mean, that the owner is responsible for the destruction of the owned object. I distinguish the following rules about arguments: take ownership don't take ownership share and about return value: release ('return by value' is in this group) don't release share For example, passing object by reference doesn't take it's ownership: void func(object & obj) { ... } Such guidelines may

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

那年仲夏 提交于 2019-12-03 01:21:02
What is the proper way of transferring ownership of a std::vector<unique_ptr<int> > to a class being constructed? Below is a code representation of what I want to do. I realize it is not correct (won't compile) and violates "uniqueness" whether I pass the vector to the constructor by value or by reference. I want Foo to be the new owner of the vector, and want the calling function to relinquish ownership. Do I need the constructor to take a std::unique_ptr<std::vector<std::unique_ptr<int> > > to do this? Foo.h class Foo { public: Foo(vector<std::unique_ptr<int> > vecOfIntPtrsOwnedByCaller);

single vs shared ownership meaning

你说的曾经没有我的故事 提交于 2019-12-02 20:53:07
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? 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

Ownership with a physical representation

为君一笑 提交于 2019-12-02 09:19:41
问题 After reading on RAII, viewing Herb Sutter's CppCon2014 presentation, and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B represent physical entities, and there's a Scene class and a Process class. The Process class is a main function, if you will. In the real world, an A can acquire a B and physically keep it for itself. It can also release it. During the course of a

Is returning a reference ever a good idea?

Deadly 提交于 2019-12-02 06:57:31
问题 We all know that returning a reference to a local variable is a bad idea. However, I'm wondering if it's ever really a good idea to a return a reference at all and if it's possible to determine some good rules about when or when not to do it. My problem with returning a reference is that the calling function needs to care about the lifetime of an object that shouldn't be its responsibility. As a contrived example: #include <vector> const int& foo() { std::vector<int> v = {1, 2, 3, 4, 5};

Ownership with a physical representation

一个人想着一个人 提交于 2019-12-02 06:46:35
After reading on RAII, viewing Herb Sutter's CppCon2014 presentation , and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B represent physical entities, and there's a Scene class and a Process class. The Process class is a main function, if you will. In the real world, an A can acquire a B and physically keep it for itself. It can also release it. During the course of a Process instance, an A object must therefore be able to have for itself a B instance, and also to release