I'm trying to wrapping an object and want to pass it as a pointer to functions. The object is known can be cast to limited types and I want to provide cast operator for these types. For example:
class MyInt {
public:
MyInt() {}
MyInt(int val): int32_storage_(val), int64_storage_(val*2) {}
const int32_t* GetInt32Ptr() const { return &int32_storage_; }
const int64_t* GetInt64Ptr() const { return &int64_storage_; }
private:
int32_t int32_storage_ = 0;
int64_t int64_storage_ = 0;
};
int32_t int32_add(const int32_t* iptr_a, const int32_t* const iptr_b) {
return *iptr_a + *iptr_b;
}
int64_t int64_add(const int64_t* iptr_a, const int64_t* const iptr_b) {
return *iptr_a + *iptr_b;
}
int main() {
MyInt a(10);
MyInt b(20);
std::cout << "32bit a + b = " << int32_add(a.GetInt32Ptr(), b.GetInt32Ptr()) << std::endl;
std::cout << "64bit a + b = " << int64_add(a.GetInt64Ptr(), b.GetInt64Ptr()) << std::endl;
return 0;
}
What I expected is to replace GetInt32Ptr()
with a cast operator so I can call int32_add()
like this int32_add(&a, &b)
. I tried
const int32_t*() const { return &int32_storage_; }
const int64_t*() const { return &int64_storage_; }
but it doesn't work.
You're almost there, the correct syntax for the operator definition is:
operator const int32_t*() const { return &int32_storage_; }
operator const int64_t*() const { return &int64_storage_; }
Also note that as described here, you can also make these operators explicit
, which is often desired to guard against unwanted conversions. It requires more verbosity, when doing the conversion, e.g. static_cast<const int32_t*>(a)
instead of just a
.
When you want the type to implicitly convert into another you have to declare that as an operator
method:
operator const int32_t*() const { return &int32_storage_; }
operator const int64_t*() const { return &int64_storage_; }
Now, in order to call the functions just say a, b
and they are implicitly converted:
std::cout << "32bit a + b = " << int32_add(a, b) << std::endl;
std::cout << "32bit a + b = " << int64_add(a, b) << std::endl;
Note: your function qualifiers aren't consistent (const int32_t*
)
they all should be const T* const
also std::endl
is generally wrong, replace it with '\n'
- reasoning
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With