Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. I just saw this beauty today...

I just saw this beauty today...

Scheduled Pinned Locked Moved The Weird and The Wonderful
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E El Bob O

    if (this == null) return;

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #4

    In C++ that makes sense as this can be NULL in some situations. :)

    Navaneeth How to use google | Ask smart questions

    D 1 Reply Last reply
    0
    • N N a v a n e e t h

      In C++ that makes sense as this can be NULL in some situations. :)

      Navaneeth How to use google | Ask smart questions

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #5

      My c++ is obviously rusty, what situations are those?

      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

      W 1 Reply Last reply
      0
      • D Dan Neely

        My c++ is obviously rusty, what situations are those?

        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

        W Offline
        W Offline
        WilliamSauron
        wrote on last edited by
        #6

        Call a method passing a null object... In Delphi, the Free method checks that it was called for an object that was really created. It's quite useful, since you can write "MyObject.Free" instead of "if Assigned(MyObject) then MyObject.Free;". A real time saver, and code is so much more readable.

        -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

        D 1 Reply Last reply
        0
        • W WilliamSauron

          Call a method passing a null object... In Delphi, the Free method checks that it was called for an object that was really created. It's quite useful, since you can write "MyObject.Free" instead of "if Assigned(MyObject) then MyObject.Free;". A real time saver, and code is so much more readable.

          -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #7

          C++ lets you set the this object in the parameters instead of always using the object the method is called on?

          Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

          W N 2 Replies Last reply
          0
          • D Dan Neely

            C++ lets you set the this object in the parameters instead of always using the object the method is called on?

            Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

            W Offline
            W Offline
            WilliamSauron
            wrote on last edited by
            #8

            No, but if you write:

            int foo ()
            {
            string *s = null;
            return s->length();
            }

            the string::length function will be called with this==null. This could be used so that the null string would behave as the empty string.

            -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

            D J 2 Replies Last reply
            0
            • W WilliamSauron

              No, but if you write:

              int foo ()
              {
              string *s = null;
              return s->length();
              }

              the string::length function will be called with this==null. This could be used so that the null string would behave as the empty string.

              -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #9

              OK, that makes sense.

              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

              1 Reply Last reply
              0
              • D Dan Neely

                C++ lets you set the this object in the parameters instead of always using the object the method is called on?

                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #10

                William has already explained it neatly. Here is another example if you are interested.

                class Dummy{
                public:
                void CheckNull(){
                if(!this)
                std::cout << "This is NULL!";
                }
                };

                Dummy* dummy = 0;
                dummy->CheckNull();

                You can see This is NULL message appearing in the above example. Above code is problematic and as per C++ standard, calls on an uninitialized pointer will lead into unexpected results.

                dan neely wrote:

                C++ lets you set the this object in the parameters instead of always using the object the method is called on?

                No. AFAIK, compiler compiles the CheckNull() method like

                void Dummy_CheckNull(Dummy* this);

                and the Dummy instance we created will be passed to this method. Your code will work fine if you are not using the this. Any operation on this will fail as it is not initialized. Hope it is clear now! :)

                Navaneeth How to use google | Ask smart questions

                T 1 Reply Last reply
                0
                • W WilliamSauron

                  No, but if you write:

                  int foo ()
                  {
                  string *s = null;
                  return s->length();
                  }

                  the string::length function will be called with this==null. This could be used so that the null string would behave as the empty string.

                  -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                  J Offline
                  J Offline
                  Jim Crafton
                  wrote on last edited by
                  #11

                  Thereby obscuring the real problem, which is that you have a null object instance you're trying to use. Loads of fun tracking down something like that.

                  ¡El diablo está en mis pantalones! ¡Mire, mire! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog Just Say No to Web 2 Point Oh

                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    William has already explained it neatly. Here is another example if you are interested.

                    class Dummy{
                    public:
                    void CheckNull(){
                    if(!this)
                    std::cout << "This is NULL!";
                    }
                    };

                    Dummy* dummy = 0;
                    dummy->CheckNull();

                    You can see This is NULL message appearing in the above example. Above code is problematic and as per C++ standard, calls on an uninitialized pointer will lead into unexpected results.

                    dan neely wrote:

                    C++ lets you set the this object in the parameters instead of always using the object the method is called on?

                    No. AFAIK, compiler compiles the CheckNull() method like

                    void Dummy_CheckNull(Dummy* this);

                    and the Dummy instance we created will be passed to this method. Your code will work fine if you are not using the this. Any operation on this will fail as it is not initialized. Hope it is clear now! :)

                    Navaneeth How to use google | Ask smart questions

                    T Offline
                    T Offline
                    TheScientistIsDead
                    wrote on last edited by
                    #12

                    would this be another example?

                    class NullCall {
                    public:
                    char PublicMethod(int x) {
                    return (char)x; }
                    };

                    NullCall::PublicMethod(12);

                    I.E. using the namespace/scope operator '::' ? -Adam

                    N 1 Reply Last reply
                    0
                    • T TheScientistIsDead

                      would this be another example?

                      class NullCall {
                      public:
                      char PublicMethod(int x) {
                      return (char)x; }
                      };

                      NullCall::PublicMethod(12);

                      I.E. using the namespace/scope operator '::' ? -Adam

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #13

                      TheScientistIsDead wrote:

                      would this be another example?

                      I don't think so. Your code produces error " error: cannot call member function ‘char NullCall::PublicMethod(int)’ without object". You can compile this by making the method as static but you can't use this inside a static method. :)

                      Navaneeth How to use google | Ask smart questions

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups