Non-initialized class

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

    Non-initialized class

    Hello,

    I encountered something unfamiliar to me today and I would like some
    clarifications. Unfortunately I don't have access to a standard but
    even if I did, I'm not even sure if I could understand it well enough
    to answer my question...

    Here's the deal: without instantiating the class, the code in question
    calls both static and non-static member functions like this:

    class foo {
    public:
    static void bar() { std::cout << "bar" << std::endl; }
    void bar2() { std::cout << "bar2" << std::endl; }
    };

    void
    func(foo* f)
    {
    f->bar();
    f->bar2();
    }

    int
    main()
    {
    func(NULL);
    return 0;
    }

    Is this allowed? And if it is, does a situation exist where it would
    be an advisable way of doing things?
  • Pete Becker

    #2
    Re: Non-initialized class

    On 2008-10-28 15:53:29 -0400, ejstans <j.l.olsson@gma il.comsaid:
    >
    Here's the deal: without instantiating the class, the code in question
    calls both static and non-static member functions like this:
    You don't have to have any instances of a class to call static member
    functions. Calling non-static member functions through a null pointer
    produces undefined behavior.
    >
    Is this allowed? And if it is, does a situation exist where it would
    be an advisable way of doing things?
    No.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)

    Comment

    • Pete Becker

      #3
      Re: Non-initialized class

      On 2008-10-28 16:05:49 -0400, Pete Becker <pete@versatile coding.comsaid:
      On 2008-10-28 15:53:29 -0400, ejstans <j.l.olsson@gma il.comsaid:
      >
      >>
      >Here's the deal: without instantiating the class, the code in question
      >calls both static and non-static member functions like this:
      >
      You don't have to have any instances of a class to call static member
      functions. Calling non-static member functions through a null pointer
      produces undefined behavior.
      >>
      >Is this allowed? And if it is, does a situation exist where it would
      >be an advisable way of doing things?
      >
      No.
      That is, "No" to calling non-static member functions without an object.
      Static member functions are fine.

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

        #4
        Re: Non-initialized class

        On 2008-10-28 20:53, ejstans wrote:
        Hello,
        >
        I encountered something unfamiliar to me today and I would like some
        clarifications. Unfortunately I don't have access to a standard but
        even if I did, I'm not even sure if I could understand it well enough
        to answer my question...
        >
        Here's the deal: without instantiating the class, the code in question
        calls both static and non-static member functions like this:
        >
        class foo {
        public:
        static void bar() { std::cout << "bar" << std::endl; }
        void bar2() { std::cout << "bar2" << std::endl; }
        };
        >
        void
        func(foo* f)
        {
        f->bar();
        f->bar2();
        }
        >
        int
        main()
        {
        func(NULL);
        return 0;
        }
        >
        Is this allowed? And if it is, does a situation exist where it would
        be an advisable way of doing things?
        No, this is not allowed. The reason it works is because you are not
        trying to access any class member and the way the compiler generates to
        code. It is never advisable to do something like this.

        Note though that you can always access static member functions without
        an object using the foo::bar() notation.

        --
        Erik Wikström

        Comment

        • Victor Bazarov

          #5
          Re: Non-initialized class

          Erik Wikström wrote:
          On 2008-10-28 20:53, ejstans wrote:
          >Hello,
          >>
          >I encountered something unfamiliar to me today and I would like some
          >clarifications . Unfortunately I don't have access to a standard but
          >even if I did, I'm not even sure if I could understand it well enough
          >to answer my question...
          >>
          >Here's the deal: without instantiating the class, the code in question
          >calls both static and non-static member functions like this:
          >>
          >class foo {
          >public:
          > static void bar() { std::cout << "bar" << std::endl; }
          > void bar2() { std::cout << "bar2" << std::endl; }
          >};
          >>
          >void
          >func(foo* f)
          >{
          > f->bar();
          > f->bar2();
          >}
          >>
          >int
          >main()
          >{
          > func(NULL);
          > return 0;
          >}
          >>
          >Is this allowed? And if it is, does a situation exist where it would
          >be an advisable way of doing things?
          >
          No, this is not allowed. The reason it works is [..]
          <rant>
          The behaviour is undefined. Any attempt to explain why undefined
          behaviour actually creates an illusion (yes, an illusion) of a working
          program only contributes to the confusion. Now the OP might think,
          "it's OK if <blahblah>". But the actual mantra should be "no, it's not
          OK, ever". It's similar to "When/Why is it OK to point an unloaded
          weapon at your friend's head?" Well, it's NOT. Ever. So, the premise
          here ("it works") is wrong. It does NOT work. It's not defined to.
          </rant>

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask

          Comment

          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

            #6
            Re: Non-initialized class

            On 2008-10-28 21:13, Pete Becker wrote:
            On 2008-10-28 16:05:49 -0400, Pete Becker <pete@versatile coding.comsaid:
            >
            >On 2008-10-28 15:53:29 -0400, ejstans <j.l.olsson@gma il.comsaid:
            >>
            >>>
            >>Here's the deal: without instantiating the class, the code in question
            >>calls both static and non-static member functions like this:
            >>
            >You don't have to have any instances of a class to call static member
            >functions. Calling non-static member functions through a null pointer
            >produces undefined behavior.
            >>>
            >>Is this allowed? And if it is, does a situation exist where it would
            >>be an advisable way of doing things?
            >>
            >No.
            >
            That is, "No" to calling non-static member functions without an object.
            Static member functions are fine.
            But you would have to use the Class::Function () notation right? I assume
            that that 9.4/2 applies to static member functions as well, more
            specifically: "A static member may be referred to using the class member
            access syntax, in which case the object-expression is evaluated."

            --
            Erik Wikström

            Comment

            • ejstans

              #7
              Re: Non-initialized class

              On 28 Okt, 21:17, Erik Wikström <Erik-wikst...@telia. comwrote:
              On 2008-10-28 20:53, ejstans wrote:
              >
              >
              >
              Hello,
              >
              I encountered something unfamiliar to me today and I would like some
              clarifications. Unfortunately I don't have access to a standard but
              even if I did, I'm not even sure if I could understand it well enough
              to answer my question...
              >
              Here's the deal: without instantiating the class, the code in question
              calls both static and non-static member functions like this:
              >
              class foo {
              public:
                  static void bar() { std::cout << "bar" << std::endl; }
                  void bar2() { std::cout << "bar2" << std::endl; }
              };
              >
              void
              func(foo* f)
              {
                  f->bar();
                  f->bar2();
              }
              >
              int
              main()
              {
                 func(NULL);
                 return 0;
              }
              >
              Is this allowed? And if it is, does a situation exist where it would
              be an advisable way of doing things?
              >
              No, this is not allowed. The reason it works is because you are not
              trying to access any class member and the way the compiler generates to
              code. It is never advisable to do something like this.
              >
              Note though that you can always access static member functions without
              an object using the foo::bar() notation.
              Thanks for the quick replies (that goes to everyone of course).

              I've got the gist of the matter, but just to cross all the i's and dot
              all the t's, is it in fact valid according to the standard to call a
              static member function through a NULL pointer? But invalid (undefined
              result?) to do so with a non-static function, even if it doesn't touch
              any member data? This is just to satisfy my curiousity, I don't like
              this construction and don't plan on using it myself...

              Comment

              • ejstans

                #8
                Re: Non-initialized class

                On 28 Okt, 22:12, Victor Bazarov <v.Abaza...@com Acast.netwrote:
                Erik Wikström wrote:
                On 2008-10-28 20:53, ejstans wrote:
                Hello,
                >
                I encountered something unfamiliar to me today and I would like some
                clarifications. Unfortunately I don't have access to a standard but
                even if I did, I'm not even sure if I could understand it well enough
                to answer my question...
                >
                Here's the deal: without instantiating the class, the code in question
                calls both static and non-static member functions like this:
                >
                class foo {
                public:
                    static void bar() { std::cout << "bar" << std::endl; }
                    void bar2() { std::cout << "bar2" << std::endl; }
                };
                >
                void
                func(foo* f)
                {
                    f->bar();
                    f->bar2();
                }
                >
                int
                main()
                {
                   func(NULL);
                   return 0;
                }
                >
                Is this allowed? And if it is, does a situation exist where it would
                be an advisable way of doing things?
                >
                No, this is not allowed. The reason it works is [..]
                >
                <rant>
                The behaviour is undefined.  Any attempt to explain why undefined
                behaviour actually creates an illusion (yes, an illusion) of a working
                program only contributes to the confusion.  Now the OP might think,
                "it's OK if <blahblah>".  But the actual mantra should be "no, it's not
                OK, ever".  It's similar to "When/Why is it OK to point an unloaded
                weapon at your friend's head?"  Well, it's NOT.  Ever.  So, the premise
                here ("it works") is wrong.  It does NOT work.  It's not defined to.
                </rant>
                Heh! Thanks! I was after this kind of clear-cut answer; that's why I
                persisted with the second question as to the actual "legality" of it,
                unfortunately posted before I could see your reply.

                Comment

                • Pete Becker

                  #9
                  Re: Non-initialized class

                  On 2008-10-28 17:17:36 -0400, Erik Wikström <Erik-wikstrom@telia. comsaid:
                  On 2008-10-28 21:13, Pete Becker wrote:
                  >>
                  >That is, "No" to calling non-static member functions without an object.
                  >Static member functions are fine.
                  >
                  But you would have to use the Class::Function () notation right? I assume
                  that that 9.4/2 applies to static member functions as well, more
                  specifically: "A static member may be referred to using the class member
                  access syntax, in which case the object-expression is evaluated."
                  Yes, I think you're right. I wasn't referring to obj->f() (where f is a
                  static member function), but to C::f() even when no object of type C
                  exists. That's not what the code in the original question did, so I may
                  have confused folks.

                  --
                  Pete
                  Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                  Standard C++ Library Extensions: a Tutorial and Reference
                  (www.petebecker.com/tr1book)

                  Comment

                  • Michael

                    #10
                    Re: Non-initialized class

                    ejstans wrote:
                    Hello,
                    >
                    I encountered something unfamiliar to me today and I would like some
                    clarifications. Unfortunately I don't have access to a standard but
                    even if I did, I'm not even sure if I could understand it well enough
                    to answer my question...
                    >
                    Here's the deal: without instantiating the class, the code in question
                    calls both static and non-static member functions like this:
                    >
                    class foo {
                    public:
                    static void bar() { std::cout << "bar" << std::endl; }
                    void bar2() { std::cout << "bar2" << std::endl; }
                    };
                    >
                    void
                    func(foo* f)
                    {
                    f->bar();
                    f->bar2();
                    }
                    >
                    int
                    main()
                    {
                    func(NULL);
                    return 0;
                    }
                    >
                    Is this allowed? And if it is, does a situation exist where it would
                    be an advisable way of doing things?
                    In this case, you are trying to dereference a NULL pointer, which will
                    produce undefined behaviour.

                    Comment

                    • Old Wolf

                      #11
                      Re: Non-initialized class

                      On Oct 29, 10:18 am, ejstans <j.l.ols...@gma il.comwrote:
                      I've got the gist of the matter, but just to cross all the i's and dot
                      all the t's, is it in fact valid according to the standard to call a
                      static member function through a NULL pointer?
                      No.


                      Comment

                      Working...