Implicit conversion
I have a Position_Component and Velocity_Component that both contain a vec3f.
I want to be able to use operators on these components and similar components i will make in the future as if they are vec3f, so i've defined an implicit conversion to a vec3f for both of these types of components. When i do try to use operators on them as if they are vec3f, i get errors that the operators are not defined.
struct Position_Component : public Component<Position_Component>
{
math::vec3f position;
Position_Component() : position(math::vec3f{ 0.0, 0.0, 0.0 }) {}
Position_Component(const math::vec3f& position) : position(position) {}
operator math::vec3f() { return position; };
};
struct Velocity_Component : public Component<Velocity_Component>
{
math::vec3f velocity;
Velocity_Component() : velocity(math::vec3f{ 0.0, 0.0, 0.0 }) {}
Velocity_Component(const math::vec3f& velocity) : velocity(velocity) {}
operator math::vec3f() { return velocity; };
};
I dont get any errors if i explicitly cast the components to vec3f before adding them.
Edit: I release i want the implicit conversion to return a reference, but this is not why im getting an error.
More code in comments.
Posted Jul 26, 2019, 1:29 PM UTC · edited
What do you mean adding them? That sounds important. What’s the function you’re trying them in?
With p being a position component and v being a velocity component, im trying to do this:
instead of this
or
The full function is
Okay and where are the definitions to add/multiply/assign? Are they functions that can be found during ADL and overload resolution?
Here is my vec3f struct: