Reputation: 48
Here are some sample classes to reproduce the situation I'm facing:
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'
I know it can be fixed easily with:
Read(std::uintptr_t(obj.seccond), sizeof(int), &buff);
but this is not satisfying enough, since it lowers code readability and it's just ugly. I really don't want to use templates since they're already everywhere all over my code. Is there any simple container for such variables? Or some more elegant way of doing it?
EDIT: I've re-adapted question, sorry
Upvotes: 1
Views: 2859
Reputation: 743
I suppose you could use a void pointer as an argument.For example:
static NTSTATUS Read(void* address, size_t size, PVOID buff);
But you should probably pass an other argument to the function in order to determine the type that you should cast the address variable to.So for example:
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)
}
}
Upvotes: 0
Reputation: 4078
You are using one int*
since your function wait for uintptr_t
.
One is signed, the other is unsigned.
Try to use unsigned
instead of int
or try to use std::intptr_t
instead of std::uintptr_t
EDIT : A way to solve your problem could be something like that :
class B {
int *number;
operator int*() {
return number;
}
};
After, you can do something like :
Read(*objA.second, ...);
The answer from Jarod42 can be good as well !
Upvotes: 1
Reputation: 217810
You might use overload to do the conversion at one place
static NTSTATUS Read(const void* p, size_t size, PVOID buff)
{
return Read(std::uintptr_t(p), size, buff);
}
Upvotes: 1