Giter Club home page Giter Club logo

self's Introduction

C++ self keyword

self is proposed keyword used to refer the current object in C++ (the same as *this). self makes more sense for the language because of the following reasons

  1. You can not change the pointer of self because it is a reference. this has a special rule that makes it a prvalue, but self does not need any exceptions.
  2. this is a special pointer that must point to a valid object however other pointers are not required to do so. References must point to a valid object so self is more suited for this purpose than this.
  3. return self instead of return *this from assignment operators looks cleaner.
  4. Using raw pointers in C++ is usually discouraged (references are preferred). this could encourage programmers to use pointers instead of references.
  5. It is easier to teach new programmers how to use references instead of pointers.
  6. Ref-qualifiers already qualify the type of this reference not pointer so using self reference instead of this pointer makes more sense.

this points to current object while self references it. self should not break the C code that is being compiled with a C++ compiler (e.g. Visual Studio). Lots of C projects use self as name for a variable that points to the current object so self should not be a reserved keyword (just like override or final).

History of this

Bjarne Stroustrup created "C with classes" in 1979. It was a superset of C and had a this pointer in it. "C with classes" did not have references. When "C with classes" evolved to C++ in 1983 references were added to the language but it was decided to leave this as a pointer for backwards compatibility. Stroustrup himself has said that the only reason why this is not a reference is because it was introduced when C++ had no references (http://www.stroustrup.com/bs_faq2.html#this).

Usage

Add #include "self" or #define self (*this) to the top of your source files or pass -include self to gcc/clang or /FI self to Visual C++ compiler.

Examples

class Foo
{
    Foo& operator=(const Foo& other)
    {
        // do the assignment
        return self;
    }
};

Proposal for the C++ standard

TODO

self's People

Contributors

elnormous avatar

Stargazers

Yohanan avatar Paul T avatar Agris Dumins avatar Elias Ku avatar Ģirts Ķesteris avatar Lucien Cartier-Tilet avatar Arvīds Kokins avatar

Watchers

James Cloos avatar Yohanan avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.