这里有一些示例类来重现我面临的情况:
class A {
B* seccond;
}
class B {
int * number;
}
static NTSTATUS Read(std::uintptr_t address, size_t size, PVOID buff);
// Let's assume we have object 'obj' which has valid address of number variable
int buff;
Read(obj.seccond, sizeof(int), &buff);
Error: cannot convert argument 1 from 'int *' to 'uintptr_t'
我知道它可以很容易地修复:
Read(std::uintptr_t(obj.seccond), sizeof(int), &buff);
但这并不令人满意,因为它降低了代码的可读性,而且只是丑陋。我真的不想使用模板,因为它们已遍布我的代码。这些变量有没有简单的容器?或者更优雅的做法呢?
编辑:我已经重新调整了问题,对不起
您可以使用重载在一个地方进行转换
static NTSTATUS Read(const void* p, size_t size, PVOID buff)
{
return Read(std::uintptr_t(p), size, buff);
}
你正在使用一个int*
,因为你的功能等待uintptr_t
。一个是签名的,另一个是未签名的。
尝试使用unsigned
而不是int
或尝试使用std::intptr_t
而不是std::uintptr_t
编辑:解决问题的方法可能是这样的:
class B {
int *number;
operator int*() {
return number;
}
};
之后,您可以执行以下操作:Read(*objA.second, ...);
Jarod42的答案也很好!
我想你可以使用void指针作为参数。例如:
static NTSTATUS Read(void* address, size_t size, PVOID buff);
但是你应该将另一个参数传递给函数,以便确定你应该将地址变量转换为的类型。例如:
static NTSTATUS Read(void* address,unsigned code, size_t size, PVOID buff)
{
//code could be int=1,float=2,...
switch(code)
{
//handle each case int,float,custom type ...
//by casting the address to the corresponding type(code)
}
}