Questions tagged [ownership-semantics]

Ownership semantics are a set of rules that govern the lifetime of allocated resources. Ownership semantics determine when and how allocated resources can be freed, and whether ownership can be shared.

67 questions
1
vote
3 answers

Move semantics in QObject::moveToThread

In the documentation of the QThread class one exemplary setup works like so: public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); //some connects to thread and worker …
Sty
  • 760
  • 1
  • 9
  • 30
1
vote
0 answers

Why does the compiler complain about borrowing a field as immutable even though my function does not access that field in any way?

I am trying to program an application that listens for requests on a socket and then adds them onto a queue to be processed. Part of my code goes as follows: pub struct Middleware { listener: TcpListener, queue: Vec, servers:…
j_beck
  • 64
  • 1
  • 5
1
vote
1 answer

Using a Generic Trait over S enforces me to have T outlive S

The boiled-down problem looks as follows: use std::marker::PhantomData; struct WorldState<'a> { state: &'a f64, } trait CalculateWorldState { fn state_value(&mut self, input: &T) -> f64; } trait LearningAlgorithm { fn…
kave
  • 461
  • 1
  • 6
  • 17
0
votes
3 answers

Handling the validity of the object of a class stored in a different class

I am trying to understand the preferred approach for a class to handle the validity of (reference to) the object of another class. In here, C has a vector that stores references of D objects. If D and C are a part of the library, how should C handle…
xyf
  • 664
  • 1
  • 6
  • 16
0
votes
0 answers

Ownership not delegated to a function using unique_ptr < T > && and std::move ( )

I was reading about passing unique_ptr to a function here: Can I pass a unique_ptr's reference to a function? The winner answer tell that this function takes ownership: void func1(unique_ptr&& moved_obj) // this function takes ownership { …
0
votes
2 answers

Is there a way to delete a pointer that has not been assigned with new operator in the destructor? If so, should I delete it in the destructor?

For example, class Test{ private: int* foo; public: Test(int* foo){this->foo = foo;} } In this case, is there any way I can delete foo in the destructor? Will I have to delete foo in the destructor or at least set it to nullptr?
OneMoreGamble
  • 117
  • 1
  • 1
  • 5
0
votes
2 answers

How to keep one object alive as long as another object exists?

Is there a way to prevent one particular object of being destroyed while another object still exists, without explicitly making the class of the second one to know about the first? Here is an example: class A { /* A creates B*/ }; class B {}; int…
nickname
  • 35
  • 6
0
votes
0 answers

Ownership sharing with smart pointers while object initialization

I have a class A that has a member pointer to class B: class A { public: A() { m_b = createB(); } std::shared getSpB() { return m_b; } private: std::shared m_b; }; class B { public: B()…
0
votes
1 answer

Smart pointer concepts ownership and lifetime

There are two concepts (ownership, lifetime) that are important when using C++ smart pointers (unique, shared, weak). I try to understand those concepts and how they influence smart pointer (or raw pointer) usage. I read two rules: Always use smart…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
0
votes
1 answer

Access to "parent" or "owner" function C++

I have a class, Game, which has in argument a std::vector of another class, Case. In this class Case, I have a function which tells me when we click on it. And I want to send a signal to my object Game, which owns the Cases, when a Case is…
GtN
  • 57
  • 4
0
votes
3 answers

Can I check whether `shared_from_this` is safe to call?

When calling shared_from_this from within types that inherit from enable_shared_from_this, very bad things (TM) can happen, if this is not currently held by a shared_ptr object (typically, the application crashing). Is it possible in C++14 (not 17)…
bitmask
  • 32,434
  • 14
  • 99
  • 159
0
votes
1 answer

Why doesn't Option::map take ownership in a linked list implementation of Iterator::next?

I'm trying to follow along Rust With Entirely Too Many Linked Lists. type Link = Option>>; pub struct List { head: Link, } struct Node { elem: T, next: Link, } pub struct Iter { next:…
asp5
  • 371
  • 1
  • 10
0
votes
1 answer

Returning reference to generic type with generic Fn trait/value

I'm just starting to learn Rust and working through the Rust book. One of the chapters leads through a few examples and ends with a "try to make this generic" type of suggested exercises. I've been totally banging my head on this. The semi-generic…
Chris
  • 154
  • 1
  • 9
0
votes
3 answers

Smart pointers ownership semantics and equality

I have a couple of questions for smart pointers that earlier I didn't give them any credit. What does mean to own an object , to point to a object and to manage a object in the world of smart pointers? Earlier I thought that the one who owns the…
Alek
  • 91
  • 8
0
votes
0 answers

Encoding stronger safety of the WinApi through the Rust FFI

I'm playing around with the winapi crate, but it doesn't seem to me to add safety to the Windows API - it seems merely to provide the types and signatures and allows us to program in mostly the same unsafe paradigms, but using Rust syntax. Is it…
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125