Static memfun through null ptr

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

    Static memfun through null ptr

    Hello all,

    is it legal to call a static member function with a null pointer?

    Is this correct C++?

    class X {
    public:
    static void f() {}
    };

    int main() {
    X* pX = 0;
    pX->f();
    }

    Thanks Oliver
  • Gianni Mariani

    #2
    Re: Static memfun through null ptr

    Oliver Anhuth wrote:[color=blue]
    > Hello all,
    >
    > is it legal to call a static member function with a null pointer?
    >
    > Is this correct C++?
    >
    > class X {
    > public:
    > static void f() {}
    > };
    >
    > int main() {
    > X* pX = 0;
    > pX->f();
    > }
    >
    > Thanks Oliver[/color]

    No, it is not legal.

    The behaviour is undefined.

    Comment

    • Ron Natalie

      #3
      Re: Static memfun through null ptr


      "Oliver Anhuth" <q4708253@bonsa i.fernuni-hagen.de> wrote in message news:bjmjpn$l6p $1@beech.fernun i-hagen.de...[color=blue]
      > Hello all,
      >
      > is it legal to call a static member function with a null pointer?
      >[/color]
      No. It is illegal to dereference, that is apply * or -> to a null pointer
      even if the result would appear not to care about the actual value of
      the pointer.


      Comment

      • Howard

        #4
        Re: Static memfun through null ptr


        "Oliver Anhuth" <q4708253@bonsa i.fernuni-hagen.de> wrote in message
        news:bjmjpn$l6p $1@beech.fernun i-hagen.de...[color=blue]
        > Hello all,
        >
        > is it legal to call a static member function with a null pointer?
        >
        > Is this correct C++?
        >
        > class X {
        > public:
        > static void f() {}
        > };
        >
        > int main() {
        > X* pX = 0;
        > pX->f();
        > }
        >
        > Thanks Oliver[/color]

        No, it's not legal, but if it's static then you don't need an instance
        variable at all. Just call it like this:

        X::f();

        -Howard




        Comment

        Working...