从 C++ 中指向地址的指针获取常量

问题描述

我正在处理一些嵌入式代码,但必须在 Linux 桌面上运行单元测试。在嵌入式代码中,我使用了固定的内存地址,但在 Linux 中不能使用相同的地址/编号。

所以在嵌入式代码/设备中我有这种形式的代码

class Foo
{
    private:
        const static uint64_t ADDRESS_TO_USE = 0xdeadbeef;

我正在努力 使用这种形式的代码解决这个问题:

uint8_t biglumpOfMemory[0x100000];

class MockFoo
{
    private:
        const static uint64_t ADDRESS_TO_USE = reinterpret_cast<const uint64_t>(&biglumpOfMemory[0]);

但这失败了,因为我无法在这样的表达式中使用强制转换(我相信)。有没有规范的方法解决这个问题?

编辑编译器错误是:

error: conversion from pointer type ‘uint8_t* {aka unsigned char*}’ to arithmetic type ‘uint64_t {aka long long unsigned int}’ in a constant-expression

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)