isocpp / isocpp/CppCoreGuidelines
R: ownership of "this" pointer
Open
@hsutter is already working on this.
Since Oct 17, 2016.
review
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
Please consider this example
class Object;
void superDuperDelete(owner<Object*> p){
//some work
delete p;
//other work
}
class Object {
void deleteSuperPuper(){
if(OK()){
//either just delete
delete this;
}
else {
//or pass ownership to someone, who will delete sometime
superDuperDelete(this);
}
}
}
int main(int argc, char *argv[]){
owner<Object*> op = new Object();
Object* p = new Object();
op->deleteSuperPuper(); //this is OK
p->deleteSuperPuper(); //this is BAD
return 0;
}
how do we handle this, how "this" is interpreted in these two cases?
don't we need some way like "const" to define that "this" is owned by a function?
Something like this.
class Object {
void deleteSuperPuper() owner {
}
}
Good real-life example of this "pattern" is QObject::deleteLater() in Qt.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.