-5

I've seen this in UE4 C++ code and in SFML C++ code and I can't seem to find out what this is exactly. I've searched google and found nothing that seems relevant to what I'm looking for.

Ex. #1 MyClassObject.Myfunction().y

Ex. #2 instantiatedObject.getPosition().x

Ex. #3 if (spriteBee.getPosition().x < -100)

It's the .X & .Y part that I'm specifically asking about.

For more context, in SFML you can get an object's X or Y coordinates specifically by using .X or .Y at the end of the getPosition() member method. I want to read up on this because I find having a member method like this could be very useful but I don't know the name of what this is exactly. Any help would be greatly appreciated.

I'm well aware that if I wanted to reproduce this I could just have a getX or getY member function and call it a day but I would love to know how this specific implementation is achieved.

YOSH
  • 11
  • 2
  • 1
    Take a `struct { float x; float y; float z; }` and write a function that returns it – Big Temp May 18 '20 at 13:44
  • 1
    That's just access to class member. Same thing as you do with `.getPosition()`, but for data, not function. See the docs for [`sf::Vector2`](https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Vector2.php) class, it lists `x` and `y` as public attributes. – Yksisarvinen May 18 '20 at 13:45
  • To the person who downvoted this question - atleast mention the reason why it was downvoted and provide the oppurtunity to the questioner to modify it. Simply downvoting without comments is not going to help a new comer. You need to understand that it was easier for people who joined earlier to get up votes for their questions and answers. Now it is very difficult because many who joined earlier had already asked basic questions or answered those basic questions and they are still earning votes from those. For a new comer it is really difficult to get votes. Have some consideration for that. – ABN May 18 '20 at 14:03
  • @Yksisarvinen Yes, I've seen the doc for the Vector2 but I'm still stumped. Is that called anything specific? If not or if so how exactly would I reproduce that? For instance, hypothetically if i wanted to have a class that could get an X or Y position using a similar function. When I try to reproduce it and access a public member it doesn't work. – YOSH May 18 '20 at 14:09
  • @ABN Thank you. I am asking a genuine question and I just need some help understanding is all. – YOSH May 18 '20 at 14:10
  • @YOSH It doesn't have a name because it's nothing special. The functions return an object, and the receiving code accesses members of that object. If you're having problems doing the same with your own classes, you will get better help if you post your attempt. – molbdnilo May 18 '20 at 14:13
  • @molbdnilo Ohhhh. I facepalmed so hard. The way you write this in code seemed so cryptic and foreign to me that I went brain dead, but when you explain it like that I understand now. Thank you so much. – YOSH May 18 '20 at 14:17

1 Answers1

0

You can do the same thing yourself:

class MyVector2 {
public:
    int x;
    int y;
};

int main() {
    MyVector2 myVec {5, 10};
    std::cout << "x from myVec:" << myVec.x << "\n"
              << "y from myVec:" << myVec.y << "\n"
}

If you want to use functions as well, that's also possible:

class MyVector2 {
public:
    int x;
    int y;
};

MyVector2 getPosition() {
    return {5, 10};
}

int main() {
    std::cout << "x from function:" << getPosition().x << "\n"
              << "y from function:" << getPosition().y << "\n"
}

Your example uses slightly longer chains. If you struggle with this concept, I suggest you dump SFML for now and get a good C++ book to learn from.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • I understand the concept. I just made it more complicated than it needed to be. I just needed someone to dumb it down for me. Seeing it in code made sense but I overthought it and made it into something bigger than what it was. Thanks for y'all help. :) – YOSH May 18 '20 at 14:20
  • @YOSH It happens to the best :) Sometimes you just a need a rubber duck, because you get so focused on a problem that you stop seeing the solutions. – Yksisarvinen May 18 '20 at 14:22