Calling static function using uninitialized pointer??

Discussion in 'C++' started by qazmlp1209@rediffmail.com, Jan 17, 2006.

  1. Guest

    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() ;

    }
    -----------------
     
    , Jan 17, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

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

    A static member isn't called using the pointer. What you have is
    undefined behaviour.


    --
    Ian Collins.
     
    Ian Collins, Jan 17, 2006
    #2
    1. Advertising

  3. Markus Moll Guest

    Hi

    wrote:

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


    > staticCl* ptr ;
    > ptr->display() ;


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

    Markus
     
    Markus Moll, Jan 17, 2006
    #3
  4. Henryk Guest

    > A static member isn't called using the pointer. What you have is
    > undefined behaviour.


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

    Just to clarify: Call your method staticCl::display() ...

    --
    Henryk
     
    Henryk, Jan 17, 2006
    #4
  5. Jaspreet Guest

    wrote:
    > 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() ;
    >
    > }
    > -----------------


    Undefined behaviour. Why not use staticCl::display() ?
     
    Jaspreet, Jan 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Lagemann
    Replies:
    8
    Views:
    502
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    453
  3. Phlip
    Replies:
    7
    Views:
    285
    Phlip
    Jan 21, 2006
  4. Replies:
    2
    Views:
    526
    Jonathan Mcdougall
    Aug 6, 2007
  5. Angus
    Replies:
    3
    Views:
    698
    James Kanze
    Mar 2, 2009
Loading...

Share This Page