Calling static function using uninitialized pointer??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qazmlp1209@rediffmail.com

    #1

    Calling static function using uninitialized pointer??

    Is the following code reliable? I'm wondering whether it is valid to
    invoke the static function using uninitialized pointer.

    -----------------
    #include <iostream>

    class staticCl
    {
    public:
    static void display()
    {
    }

    } ;

    int main()
    {
    staticCl* ptr ;
    ptr->display() ;

    }
    -----------------

  • Ian Collins

    #2
    Re: Calling static function using uninitialized pointer??

    qazmlp1209@redi ffmail.com wrote:[color=blue]
    > Is the following code reliable? I'm wondering whether it is valid to
    > invoke the static function using uninitialized pointer.
    >[/color]
    A static member isn't called using the pointer. What you have is
    undefined behaviour.


    --
    Ian Collins.

    Comment

    • Markus Moll

      #3
      Re: Calling static function using uninitialized pointer??

      Hi

      qazmlp1209@redi ffmail.com wrote:
      [color=blue]
      > Is the following code reliable? I'm wondering whether it is valid to
      > invoke the static function using uninitialized pointer.[/color]
      [color=blue]
      > staticCl* ptr ;
      > ptr->display() ;[/color]

      The expression is equivalent to "(*ptr).display ()", so you invoke undefined
      behavior by dereferencing ptr.

      Markus

      Comment

      • Henryk

        #4
        Re: Calling static function using uninitialized pointer??

        > A static member isn't called using the pointer. What you have is[color=blue]
        > undefined behaviour.[/color]

        Right. The code may work with most compilers but you can not rely on
        that.

        Just to clarify: Call your method staticCl::displ ay() ...

        --
        Henryk

        Comment

        • Jaspreet

          #5
          Re: Calling static function using uninitialized pointer??

          qazmlp1209@redi ffmail.com wrote:[color=blue]
          > Is the following code reliable? I'm wondering whether it is valid to
          > invoke the static function using uninitialized pointer.
          >
          > -----------------
          > #include <iostream>
          >
          > class staticCl
          > {
          > public:
          > static void display()
          > {
          > }
          >
          > } ;
          >
          > int main()
          > {
          > staticCl* ptr ;
          > ptr->display() ;
          >
          > }
          > -----------------[/color]

          Undefined behaviour. Why not use staticCl::displ ay() ?

          Comment

          Working...