简体   繁体   English

std::dec 仍然将内存地址输出为十六进制?

[英]std::dec still outputting memory address to hex?

I have:我有:

std::cout << "Start = " << std::dec << (&myObject) << std::endl;

to output an address in decimal.以十进制输出地址。 However, the address is still coming out in hex??但是,地址仍然以十六进制形式出现??

(I am outputting one of these for each of ten members, so I don't want to assign each one to a variable and then std::dec the variable separately) (我为十个成员中的每一个输出其中一个,所以我不想将每个成员分配给一个变量,然后分别将变量std::dec分配给变量)

The hex and dec manipulators are for integers , not pointers. hexdec操作dec用于整数,而不是指针。 Pointers are always rendered in the form that printf 's %p formatter would have used on your system (which is, usually, hexadecimal notation).指针总是printf%p格式化程序在您的系统上使用的形式呈现(通常是十六进制表示法)。

This helps to emphasise the fact that pointers and numbers are distinct.这有助于强调指针和数字是不同的这一事实。 You may consider it to be one of the rare cases in which number semantics and number representation are, to some degree, coupled.您可能会认为这是数字语义和数字表示在某种程度上耦合的极少数情况之一。

The best you can do is to cast the pointer to uintptr_t before streaming it:您能做的最好的事情是在流式传输之前将指针uintptr_tuintptr_t

std::cout << "Start = " << std::dec << uintptr_t(&myObject) << std::endl;

…but please consider whether you really need to do so. ……但请考虑您是否真的需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM