isocpp / isocpp/CppCoreGuidelines

R: ownership of "this" pointer

Open
#738 26 comments 0 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.