Hello #cpp / #cplusplus Fediverse,
-
Hello #cpp / #cplusplus Fediverse,
If I've got types
struct fooandstruct bar, which are unrelated by formal class inheritance, is there some way to define a conversion fromstruct foo *tostruct bar *?)I can't attach a plain old conversion operator to a pointer type, even if I name it, and none of the C++ compilers that I tried thinks of invoking a reference conversion operator when what's supposed to be implicitly converted, and the expectant receptacle, are nominally pointers.
Is there a way?
-
undefined oblomov@sociale.network shared this topic
-
Hello #cpp / #cplusplus Fediverse,
If I've got types
struct fooandstruct bar, which are unrelated by formal class inheritance, is there some way to define a conversion fromstruct foo *tostruct bar *?)I can't attach a plain old conversion operator to a pointer type, even if I name it, and none of the C++ compilers that I tried thinks of invoking a reference conversion operator when what's supposed to be implicitly converted, and the expectant receptacle, are nominally pointers.
Is there a way?
@riley what do you mean by a conversion between pointer types?
-
@riley what do you mean by a conversion between pointer types?
@oblomov Defining a function/operator that would be implicitly invoked when
struct foo *is passed to a function declared to accept astruct bar *that would ideally amount to adding a small constant to the pointer; effectively, there's astruct barinside astruct foo, and I'd like astruct bar *to be converted tostruct foo *by implicitly referring to that inner struct when a pointer to the outer struct is being passed to a function taking a pointer to the inner struct. -
@oblomov Defining a function/operator that would be implicitly invoked when
struct foo *is passed to a function declared to accept astruct bar *that would ideally amount to adding a small constant to the pointer; effectively, there's astruct barinside astruct foo, and I'd like astruct bar *to be converted tostruct foo *by implicitly referring to that inner struct when a pointer to the outer struct is being passed to a function taking a pointer to the inner struct. -
@lesley yes, but in the case of @riley that's not the same issue: if foo has-a bar, there's no way to do the casting _implicitly_, as far as a I know. It would be possible with references by defining an implicit constructor of bar from foo, but this won't be applied to pointers.
The only work around I can think of is to define an overloaded function
bar * get_bar(bar * o) { return o; }
bar * get_bar(foo * o) { return &o->bar_element; }and wrap pointer arguments with that function.
-
-
-