Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

what exactly does the 'this' pointer does???

grahamojgrahamoj Member Posts: 366
hiya, can anyone explain to me what exactly does the 'this' pointer does???

thanks,
grahamoj

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : hiya, can anyone explain to me what exactly does the 'this' pointer does???
    :
    : thanks,
    : grahamoj
    :
    [code]
    class A
    {
    public:
    int m_iValue;

    A(int val) { m_iValue = val; }
    void foo ();
    };

    void A::foo ()
    {
    printf ("
    Value: %d", this->m_iValue);
    //printf ("
    Value: %d", m_iValue); //SAME RESULT!
    }

    void some_func ()
    {
    A a1 (5);
    A a2 (10);

    A* pa1 = &a1;
    A* pa2 = &a2;

    a1.foo (); // Prints: "5" ('this' inside it will be equal to 'pa1')
    a2.foo (); // Prints: "10" ('this' inside it will be equal to 'pa2')
    }
    [/code][blue]In other words - 'this' is a physical pointer to the instance (data) of the object when the member function called. In fact - every time member function is called - 'this' is passed as a hidden parameter.[/blue]
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ___ // http://forcoder.org // free ebooks and video tutorials about // C PHP R JavaScript Python Perl Swift MATLAB C++ Scratch Visual Basic .NET Ruby Objective-C Go Delphi PL/SQL Visual Basic Assembly Java C# Julia Erlang Crystal Lisp Apex Rust FoxPro Transact-SQL Bash Prolog SAS Awk ABAP Kotlin Alice LabVIEW Scheme Dart Lua D F# ML COBOL Logo Hack Ada Scala VBScript Clojure Fortran // ___________

Sign In or Register to comment.