Differences between C++ and Java with this-pointer

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

    Differences between C++ and Java with this-pointer


    Hello,



    I am trying to translate a C++ script to Java. Are there any differences
    between the C++ this-pointer and the Java this-pointer?



    Thank you

    Flo


    --
    Posted via http://dbforums.com
  • Victor Bazarov

    #2
    Re: Differences between C++ and Java with this-pointer

    "Rasti" <member17950@db forums.com> wrote...[color=blue]
    > I am trying to translate a C++ script to Java. Are there any differences
    > between the C++ this-pointer and the Java this-pointer?[/color]

    In Java 'this' is NOT a pointer. There are no pointers in Java.

    And, if you have questions on Java, try a Java newsgroup.

    Victor



    Comment

    • Joe Gottman

      #3
      Re: Differences between C++ and Java with this-pointer


      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
      news:28cqb.1147 45$e01.418754@a ttbi_s02...[color=blue]
      > "Rasti" <member17950@db forums.com> wrote...[color=green]
      > > I am trying to translate a C++ script to Java. Are there any differences
      > > between the C++ this-pointer and the Java this-pointer?[/color]
      >
      > In Java 'this' is NOT a pointer. There are no pointers in Java.
      >
      > And, if you have questions on Java, try a Java newsgroup.
      >[/color]

      Personally, I think that a question about the similarities and
      differences between C++ and Java is on-topic for either C++ or Java
      newsgroups. To answer the OP's question, in C++ 'this' is a pointer and in
      Java 'this' is a reference, because Java doesn't have pointers. However,
      the C++ 'this' pointer is rather strange, because it cannot be reassigned
      and cannot be null. It probably should have been a reference. As I
      understand it, the main reason it wasn't is because it was invented before
      references were.

      Joe Gottman



      Comment

      • lilburne

        #4
        Re: Differences between C++ and Java with this-pointer

        Joe Gottman wrote:
        [color=blue]
        >
        >
        > However,
        > the C++ 'this' pointer is rather strange, because it cannot be reassigned
        > and cannot be null.
        >[/color]

        void func(A& a);


        A* aptr = 0;
        func(*aptr);

        I think you'll find that within func a's this pointer will
        be null.


        Comment

        • Pete Becker

          #5
          Re: Differences between C++ and Java with this-pointer

          Victor Bazarov wrote:[color=blue]
          >
          > "Rasti" <member17950@db forums.com> wrote...[color=green]
          > > I am trying to translate a C++ script to Java. Are there any differences
          > > between the C++ this-pointer and the Java this-pointer?[/color]
          >
          > In Java 'this' is NOT a pointer. There are no pointers in Java.
          >[/color]

          There are no pointers, but there is a NullPointerExce ption.

          --

          Pete Becker
          Dinkumware, Ltd. (http://www.dinkumware.com)

          Comment

          • Joe Gottman

            #6
            Re: Differences between C++ and Java with this-pointer


            "lilburne" <lilburne@godzi lla.net> wrote in message
            news:boca9k$1c1 n7t$1@ID-203936.news.uni-berlin.de...[color=blue]
            > Joe Gottman wrote:
            >[color=green]
            > >
            > >
            > > However,
            > > the C++ 'this' pointer is rather strange, because it cannot be[/color][/color]
            reassigned[color=blue][color=green]
            > > and cannot be null.
            > >[/color]
            >
            > void func(A& a);
            >
            >
            > A* aptr = 0;
            > func(*aptr);
            >
            > I think you'll find that within func a's this pointer will
            > be null.[/color]

            Derefencing a null pointer, as you do when you call func(*aptr) results
            in undefined behavior. You might have a's this pointer null inside the
            function call, or you might have a core dump before func is even called.

            Joe Gottman


            Comment

            Working...