Integer to object pointer and back again in a function

I need to create an object and return it back as an integer anssalso convert an int to a class/object pointer in a function as a parameter and back again as integer for the result.

What is the best way to do this? Basically my ideal setup will be like this:

1
2
3
4
5
int foo; 

foo  = CreateObject(); // function declared as MyClass* CreateObject();

DoSomething(foo); //function accepting MyClass* as param 


I have seen this mostly used in C wrappers, I understand passing object or class pointers is normal in C as well but I need to pass it an integer for something that I am working on that really requires it to be integer.

not sure about the steps involved.

Last edited on
An object pointer is not guaranteed to fit into an int, but there is uintptr_t that is the right size to hold a pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

class A {
public:
    void f() { std::cout << "yo\n"; }
};

void func(uintptr_t u) {
    auto a = reinterpret_cast<A*>(u);
    a->f();
}

int main() {
    A* a = new A;
    auto u = reinterpret_cast<uintptr_t>(a);
    func(u);
    delete a;
}

Last edited on
Thanks dutch. I forgot to mention that I need it to be in C, like a wrapper to the C++ actual Class functions.

will something like this would be ok and not an issue?

1
2
3
int foo; //<-- need to declare this as int

foo = (uintptr_t)CreateObject();


and then passing to a function..

MyFunction((MyClass*)foo, x, y, z);



Last edited on
convert an int to a class/object pointer

I have no idea how you could do that but by a reinterpret_cast<>, as dutch suggested; but perhaps this code might give you some hints about alternatives:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>


struct Ruzip {
    int val {};

    Ruzip(int val_arg);         // can implicitely create a Ruzip from an int.
                                // It's like a coversion.

    /* explicit */ operator int() const;
};


Ruzip::Ruzip(int val_arg)
    : val { val_arg }
{
}


Ruzip::operator int() const
{
    return val;
}



void func(const Ruzip * const ruzip, int = 0, int = 0, int = 0);


int main()
{
    int i { Ruzip( 13 ) };
    Ruzip ruzip(i);
    func( &ruzip );
}


void func(const Ruzip * const ruzip, int, int, int)
{
    std::cout << "ruzip: " << *ruzip << '\n';
}


Output:
ruzip: 13

As Dutch said,
An object pointer is not guaranteed to fit into an int

What makes you think that a pointer will fit inside an int in your system? If you're running a 64-bit operating system then chances are excellent that an int is 32 bits and a pointer is 64 bits.

Do you really, truly, absolutely HAVE to pass it as an int? The only reason I can think of is if you're passing it to a function that already exists.

If you're writing the function then figure out how to pass the pointer as a pointer. Maybe it needs to be a void*?
Thanks Enoizat for the additional example.

Do you really, truly, absolutely HAVE to pass it as an int? The only reason I can think of is if you're passing it to a function that already exists.


Yes it already exists and is a set of functions.

Okay. In that case, just cast it with a reinterpret cast like dutch said, or a C style cast:
1
2
3
    int i = 0;
    void *vp = reinterpret_cast<void*>(i);
    i = reinterpret_cast<int>(vp);

The compiler will tell you if the sizes don't match.
Great! working on a few test, but I have not tested it thoroughly.

problem solved for now, thanks.
Topic archived. No new replies allowed.