Why "this" is a pointer, I just realized!

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

    Why "this" is a pointer, I just realized!

    You can overload the address-of operator. Consider a simple class:

    class Blah
    {
    public:

    Blah* operator&()
    {
    return this;
    }
    };

    Now... consider if "this" was a reference (as before I've argued it should
    be):

    class Blah
    {
    public:

    Blah* operator&()
    {
    return &this;
    }
    };


    looks like an infinite loop to me...


    Although to be honest I would've prefered "p_this".


    -JKop
  • David Hilsee

    #2
    Re: Why "this&quot ; is a pointer, I just realized!

    "JKop" <NULL@NULL.NULL > wrote in message
    news:Ss5%c.2673 2$Z14.8714@news .indigo.ie...[color=blue]
    > You can overload the address-of operator. Consider a simple class:
    >
    > class Blah
    > {
    > public:
    >
    > Blah* operator&()
    > {
    > return this;
    > }
    > };
    >
    > Now... consider if "this" was a reference (as before I've argued it should
    > be):
    >
    > class Blah
    > {
    > public:
    >
    > Blah* operator&()
    > {
    > return &this;
    > }
    > };
    >
    >
    > looks like an infinite loop to me...
    >
    >
    > Although to be honest I would've prefered "p_this".[/color]

    In "The Design and Evolution of C++", Bjarne Stroustrup says that "this" is
    a pointer and not a reference because references were not present in "C with
    Classes" at the time that "this" was introduced.

    --
    David Hilsee


    Comment

    • David Hilsee

      #3
      Re: Why &quot;this&quot ; is a pointer, I just realized!

      "David Hilsee" <davidhilseenew s@yahoo.com> wrote in message
      news:25KdnR1Coa exbKHcRVn-gw@comcast.com. ..[color=blue]
      > "JKop" <NULL@NULL.NULL > wrote in message
      > news:Ss5%c.2673 2$Z14.8714@news .indigo.ie...[color=green]
      > > You can overload the address-of operator. Consider a simple class:
      > >
      > > class Blah
      > > {
      > > public:
      > >
      > > Blah* operator&()
      > > {
      > > return this;
      > > }
      > > };
      > >
      > > Now... consider if "this" was a reference (as before I've argued it[/color][/color]
      should[color=blue][color=green]
      > > be):
      > >
      > > class Blah
      > > {
      > > public:
      > >
      > > Blah* operator&()
      > > {
      > > return &this;
      > > }
      > > };
      > >
      > >
      > > looks like an infinite loop to me...
      > >
      > >
      > > Although to be honest I would've prefered "p_this".[/color]
      >
      > In "The Design and Evolution of C++", Bjarne Stroustrup says that "this"[/color]
      is[color=blue]
      > a pointer and not a reference because references were not present in "C[/color]
      with[color=blue]
      > Classes" at the time that "this" was introduced.[/color]

      I did a quick search and found that many people already said this the first
      time you brought this up. I must have missed the discussion the first time
      around. I'll add something new by pointing out that your overload of
      operator& has little bearing on the matter, because it is rarely (if ever)
      used in the way that you are using it. Normally, operator& is overloaded to
      _alter_ the return value. If you just want to return "this", then you don't
      need to overload operator&.

      If you really wanted the address and wanted to avoid invoking operator&, you
      could use something like boost's addressof
      (http://www.boost.org/libs/utility/ut...htm#addressof), so it wouldn't
      matter if "this" was a reference.

      --
      David Hilsee


      Comment

      • Ioannis Vranos

        #4
        Re: Why &quot;this&quot ; is a pointer, I just realized!

        JKop wrote:[color=blue]
        > You can overload the address-of operator. Consider a simple class:
        >
        > class Blah
        > {
        > public:
        >
        > Blah* operator&()
        > {
        > return this;
        > }
        > };
        >
        > Now... consider if "this" was a reference (as before I've argued it should
        > be):
        >
        > class Blah
        > {
        > public:
        >
        > Blah* operator&()
        > {
        > return &this;
        > }
        > };
        >
        >
        > looks like an infinite loop to me...
        >
        >
        > Although to be honest I would've prefered "p_this".[/color]



        Provided the reference to "Design and Evolution" that David provided, I
        want to note that C++ is not a language of the lab, but a product of
        evolution that *addresses real world problems*.


        So here is the evolution of C++:


        BCPL


        B: As dmr mentions, "B can be thought of as C without types; more
        accurately, it is BCPL squeezed into 8K bytes of memory and filtered
        through Thompson's brain."




        C90 (ISO C90)


        C++98 (some concepts also taken from Simula).






        Regards,

        Ioannis Vranos


        Comment

        • Phlip

          #5
          Re: Why &quot;this&quot ; is a pointer, I just realized!

          Ioannis Vranos wrote:
          [color=blue]
          > Provided the reference to "Design and Evolution" that David provided, I
          > want to note that C++ is not a language of the lab, but a product of
          > evolution that *addresses real world problems*.[/color]

          Notice Ioannis is saying that C++ was deployed to business users before all
          its logical inconsistencies were removed. These tend to shock newbies who
          think that a language designer must be somehow infallible.

          --
          Phlip



          Comment

          • Jerry Coffin

            #6
            Re: Why &quot;this&quot ; is a pointer, I just realized!

            "Phlip" <phlip_cpp@yaho o.com> wrote in message news:<8cb%c.148 31$Y94.6745@new ssvr33.news.pro digy.com>...

            [ ... ]
            [color=blue]
            > Notice Ioannis is saying that C++ was deployed to business users before all
            > its logical inconsistencies were removed. These tend to shock newbies who
            > think that a language designer must be somehow infallible.[/color]

            Regardless of the method by which it was developed, I've yet to see a
            single language from which all logical inconsistencies had been
            removed, or which lacked "features" that could surprise newbies. In
            the end, language designers are human, and the languages they design
            reflect that.

            OTOH, I'd keep in mind that a language is basically a specification
            for some software. Most current thought on specs for software seems to
            emphasize collecting information from real users on a regular and
            ongoing basis throughout the design process, and IMO, a language is no
            different. If anything, I'd say that languages generally suffer more
            from language designers who are too close to infallible. They design
            languages that work well for their aims, but they're enough different
            from the average programmer that they often design things that average
            programmers can barely understand, not to mention really use.

            --
            Later,
            Jerry.

            The universe is a figment of its own imagination.

            Comment

            Working...