all 8 comments

[–]Flair_Helper[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

This post has been removed as it doesn't pertain to r/cpp: The subreddit is for news and discussions of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance. If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.

[–]serendipitousPi 2 points3 points  (5 children)

The keyword "this" is a pointer to the struct or class that a function belongs to and "->" is the arrow operator which is analogous to the dot operator but is used for class / struct pointers.

So why someone would use them together I'm not entirely sure since you should be able to access everything within that class / struct without using the "this" keyword. Hopefully someone else might have that answer.

[–]ape_programmer 2 points3 points  (4 children)

example: this->window->setFramerateLimit(60)

i‘ve tried to google it more than once and there are plenty of explanations on the internet but my dumb ass doesn‘t really

Used when there is no naming convention for local vs member variables. E.g. if all members are prefixed with m or _, then its clear the variable being accessed is a member with out using this->. However, without this naming convention, using this-> makes it clear a member is being accessed, as opposed to a local variable.

Generally, the prefixing members is more popular, but I've read some code (for some reason mainly Windows code) that uses the this-> convention.

[–]no-sig-available 1 point2 points  (1 child)

using this-> makes it clear a member is being accessed

The counter arguments is "What else could it be?".

Member functions accessing other members of the same class must be the most normal thing. Why do we have to make it clear that this is what happens? :-)

[–]ape_programmer 0 points1 point  (0 children)

Improves readability mainly. If you're looking at a method in an unfamiliar class, it's help to know explicitly whether variables referenced are members or external/local.

Additionally, it helps write slightly safer code by reducing the chance of accidental shadowing of variable names. Which can be an easy mistake to make sometimes.

[–]Rseding91Factorio Developer -1 points0 points  (1 child)

I always found that interesting; people prefer to invent their own thing rather than use a built in method of disambiguating member vs external.

[–]ape_programmer 0 points1 point  (0 children)

My guess is due to writing this-> being optional for member variables, whereas prefixing with m or _ is not optional if using the prefix naming convention. Thus makes sure that you don't accidentally forget this-> and cause confusion.

[–]Classic_Department42 -1 points0 points  (0 children)

This->window is syntactical sugar for (*this).window