Using a wrapper class as a pointer to the type it wraps?

I have a simple wrapper class for Integer types, defined like so (just an example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Integer {
    public:
        Integer() {
        }

        Integer(const int& value) {
            this->value = value;
        }

        int toInt() const {
            return value;
        }

        operator int() const {
            return toInt();
        }

    private:
        int value = 0;
};


What I'd like to do, is pass the class above to a function which has a signature like this: doSomething(int* value)

This function is outside of my control and cannot be edited (it is in an external library).
If I were to use a normal int, I could simply do:

1
2
int value = 5;
doSomething(&value);


However, when using the wrapper class I can't since it would use a pointer to the class instead of the actual underlying value. I know of the address operator operator&, which I could use to return a pointer to the value, but it would prevent me from getting a pointer to the class itself if I needed to.

So ideally there would be a way that would allow me to use &myclass to get a pointer to the class or the underlying value, depending on what is needed.

Is there such a way?

I've tried asking this question at Stack Overflow, but since they're so hell bound on knowing why I want to do this and very persistent on not answering the question but trying to circumvent it and give their own opinions on why this or that is bad, I'm going to try it here once again. Sorry for the rant, just had to get that out :)
Last edited on
closed account (E0p9LyTq)
doSomething(Integer* value)
Alas, I wish it were as simple as that. The function which requests int* is outside of my control (in the OpenGL API) and thus I cannot change it.

I'm thinking of using a function Integer* getIntPointer() to get a pointer to the value, but it seems like a bad design to me, and it would clutter the code.
Last edited on
closed account (48T7M4Gy)
Keep in mind you don't pass classes but objects.

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
42
43
44
45
46
47
48
49
#include <iostream>

class Integer {
public:
    Integer() {
    }
    
    Integer(const int& value) {
        this->value = value;
    }
    
    int toInt() const {
        return value;
    }
    
    operator int() const { // <-- ???
        return toInt();
    }
    
private:
    int value = 0;
};

void doSomething(Integer* );
void doSomething(int* );

int main()
{
    const int val = 507;
    Integer* integer = new Integer(val);
    std::cout << integer->toInt() << '\n';
    doSomething(integer);
    
    int newValue = integer -> toInt();
    doSomething(&newValue);
    return 0;
}

void doSomething(Integer* anInteger)
{
    std::cout << 2 * anInteger -> toInt() << '\n';
    return;
}

void doSomething(int* anInteger)
{
    std::cout << 3 * *anInteger << '\n';
    return;
}



507
1014
1521
Program ended with exit code: 0


Despite that, surely passing an Integer* to a function requiring an int* is an obvious waste of time.
Last edited on
What you've posted would work, it would however require me to implement a second "wrapper" function, and am I correct in understanding that it would work on a copy of the object instead of the original one? As in any changes made within the doSomething function would not be applied to the original?

What I've been thinking about doing is adding a function such as this to the wrapper:

1
2
3
explicit operator int*() {
	return &value;
}


which would allow me to use

1
2
Integer test = 5;
doSomething(static_cast<int*>(test));


Would that be considered bad code / style?
Last edited on
closed account (E0p9LyTq)
If a function requires a POD pointer (int*), wrapping it in a custom class is just overtinking the plumbing. Complicating something simple could be considered a definite waste of effort, even for educational purposes.

Casting to an int* is really over-kill IMO.
If you're going to do this, I would either use a named function like: wrappedAddress or overload the unary operator& to return the address of the wrapped object.

In case of the latter, you really don't need to worry about being able to get the address of the wrapping object - just use std::addressof when you need it (assuming you're not tied to a pre-c++11 compiler.)
Last edited on
@FurryGuy I appreciate your concerns, but the class is already being used by a large part of the project we're working on and thus cannot be replaced easily.

@cire I've thought about overloading the address operator, however it would be misleading for new people working on the code, since they get a pointer to something that they're not expecting. Ideally I would like to overload the operator so that it returns a pointer to the object itself or the value contained within, depending on the type that is expected by the code the object is used in. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
void doSomething(int* value) {
// ...
}

void doSomethingElse(Integer* value) {
// ...
}

int main() {
Integer test = 5;
doSomething(&test); // Here a pointer to the int value inside test is being passed
doSomethingElse(&test); // Here a pointer to test itself is being passed
}

However I do not know of such functionality existing in C++.
Last edited on
Topic archived. No new replies allowed.