Calling static function using uninitialized pointer??

Q

qazmlp1209

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

}
-----------------
 
I

Ian Collins

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.
 
M

Markus Moll

Hi

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
 
H

Henryk

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() ...
 
J

Jaspreet

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top