std :: dec仍然输出内存地址为十六进制? [英] std::dec still outputting memory address to hex?

查看:200
本文介绍了std :: dec仍然输出内存地址为十六进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

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

输出十进制地址。但是,地址仍然是十六进制的。



(我为十个成员中的每一个输出其中一个,所以我不想分配每个成员到变量,然后 std :: dec 单独的变量)

解决方案

hex dec 操作符用于整数,而不是指针。指针总是以 printf %p 格式化程序在你的系统上(通常是十六进制符号)。



这有助于强调指针和数字是不同的。你可以认为它是一种罕见的情况,其中数字语义和数字表示在某种程度上耦合。



你最好做的是投射在流媒体之前指向 uintptr_t

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

…但请考虑您是否真的需要这样做。 / p>

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)

解决方案

The hex and dec manipulators are for integers, not pointers. Pointers are always rendered in the form that printf's %p formatter would have used on your system (which is, usually, hexadecimal notation).

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:

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

…but please consider whether you really need to do so.

这篇关于std :: dec仍然输出内存地址为十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆