C++ DirectX Accessing a Vector through a method.

I want to access a vector through a method. How do I do it without having to access the individual floats?

Is there a way to access this float through only the:

1
2
3
4
Vector2 Projectile::ReturnPosition()
	{
		return this->position;
	}


E.g. To access the position.x position I have to create a seperate method as shown below.

1
2
3
4
5
6
7
8
9
  float Projectile::ReturnXPosition()
	{
		return this->position.x;
	}

	Vector2 Projectile::ReturnPosition()
	{
		return this->position;
	}


Thanks.
Last edited on
You cannot overload a function by only the return type.

I don't understand why you'd want to do this, since you can simply do:
position.x;

If you don't want to write that, just create a reference to the object.
Because in other parts of my code I need to return the whole Vector2 position, not just the single float, e.g. the .y and .x.

That's why I only want the:

1
2
3
4
Vector2 Projectile::ReturnPosition()
	{
		return this->position;
	}


I don't think I would want to create a reference to that object, not what I am trying to do.
Last edited on
I don't think I would want to create a reference to that object, not what I am trying to do.
I know that is not what you're trying to do, I'm saying that you should do it. I'm assuming that you want to return either a float or a structure in order to not have to write out 'position.x' every time.

So to answer your question, no you cannot. But I don't see why you can't just retrieve the x float from the structure Projectile::ReturnPositition() returns. Is there a reason you don't want to?
^ I did end up creating a reference to the object and it worked.

I just wondered if you could do it the other way. I guess not though.

Thanks.
Topic archived. No new replies allowed.