Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a pointer to std::cerr?

Given:

MY_CLASS* ptr = MY_CLASS::GetSomeInstance();

What is the correct way to output ptr to std::cerr, so I can log its value? Note I don't want to write the class, just the address.

like image 939
Justicle Avatar asked May 19 '26 07:05

Justicle


1 Answers

operator<< is overloaded to take a const void*, so you can simply insert the pointer into the stream:

std::cerr << ptr;

The exception is that if the pointer is a const char*, it will be interpreted as a pointer to a C string. To print the pointer, you need to cast it explicitly to a const void*:

std::cerr << static_cast<const void*>(ptr); 
like image 184
James McNellis Avatar answered May 20 '26 19:05

James McNellis