this pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stub

    this pointer

    Some questions about "this" pointer:

    1. To make an assignment function,

    Stribg &String::operat or=(const String &other)
    {
    ...
    return *this;
    }

    I have hard time to understand why "*this" matches a "String &" here. Could
    someone shed some light on this?

    2. In a static member function aren't there "this" pointer accessible inside
    it?

    3. In a const member function is the "this" pointer of const type while
    non-const type in a non-const member?

    Thanks in advance!






  • Ali R.

    #2
    Re: this pointer

    > Some questions about "this" pointer:[color=blue]
    >
    > 1. To make an assignment function,
    >
    > Stribg &String::operat or=(const String &other)
    > {
    > ...
    > return *this;
    > }[/color]

    The return type is a String & (String reference) and the assignment
    operator is passing back a reference to itself one the assignment is done.[color=blue]
    >
    > I have hard time to understand why "*this" matches a "String &" here.[/color]
    Could[color=blue]
    > someone shed some light on this?
    >
    > 2. In a static member function aren't there "this" pointer accessible[/color]
    inside[color=blue]
    > it?[/color]

    No there is no this pointer in a static member function.[color=blue]
    >
    > 3. In a const member function is the "this" pointer of const type while
    > non-const type in a non-const member?
    >[/color]
    Yes the this pointer is a const pointer in a const member function.
    [color=blue]
    > Thanks in advance!
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Jacek Dziedzic

      #3
      Re: this pointer

      "stub" <stub@asof.co m> wrote in message news:1qzob.2275 3
      $Ec1.2015438@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
      > Some questions about "this" pointer:
      >
      > 1. To make an assignment function,
      >
      > Stribg &String::operat or=(const String &other)
      > {
      > ...
      > return *this;
      > }
      >
      > I have hard time to understand why "*this" matches a
      > "String &" here. Could someone shed some light on this?[/color]

      You return *this in an assignment operator so that
      it's possible to do things like:

      a=b=c; // a,b,c are String's and
      // b returns (*this) so that a can be assigned
      [color=blue]
      > 2. In a static member function aren't there "this" pointer
      > accessible inside it?[/color]

      Static methods have no "this" pointer, because they
      are not associated with a particular instance of an object.

      HTH,
      - J.


      Comment

      Working...