-3

I'm using a C++ code which is coded by someone else. I want to know what is happening in this line of code.

tplayer is an array and OnTickContext is a bool variable.

 tPlayers[i].OnTickContext = (void*)this;
  • 1
    _"I want to knoww what is happening here"_ `this` is casted to a `void` pointer and assigned to `tPlayers[i].OnTickContext`. – πάντα ῥεῖ Feb 09 '19 at 09:08
  • 1
    If `OnTickContext` is a `bool` variable then nothing very sensible is happening. If `OnTickContext` was a `void*` variable it would make more sense. It would mean that the current object (i.e. the current value of `this`) is being saved for some later purpose. – john Feb 09 '19 at 09:09
  • 1
    Assigning a pointer to a `bool` will set the `bool` to false if the pointer is null, and to true otherwise. – Remy Lebeau Feb 09 '19 at 09:12
  • As @Remy said is that this is the same as: `if(this==nullptr) tPlayers[i].OnTickContext = false; else tPlayers[i].OnTickContext = true;` It's a shortcut in C++ used very often. One conversion (and only one) from one type to another is often implied - and can be a nightmare to understand at start. – Ted Lyngmo Feb 09 '19 at 09:28
  • what is the use of "this". what is the significant of "this" here. – PANKAJ BELWAL Feb 09 '19 at 09:28
  • It's the pointer to the object itself. This must be done within a method/function of a class/struct. It will always be true btw ... Do you have experience with Python, Perl or Java? – Ted Lyngmo Feb 09 '19 at 09:29
  • 2
    So what's written in code will be optimized away since it will always be true. You couid have written `tPlayers[i].OnTickContext = true`. Same thing. – Ted Lyngmo Feb 09 '19 at 09:38
  • `this` is the object whose method you are currently executing. – john Feb 09 '19 at 09:50
  • 1
    @TedLyngmo "*It will always be true btw*" - unless the method is called via a null pointer, that is. Then `this` will be null in most compilers. – Remy Lebeau Feb 09 '19 at 17:57
  • @RemyLebeau :-) true but is't that ub? – Ted Lyngmo Feb 09 '19 at 19:26
  • @TedLyngmo Technically yes, it is. But it does work in most compilers. – Remy Lebeau Feb 09 '19 at 20:14

2 Answers2

6

This looks like the programmer did not know what he was doing, or wanted to look smarter than he was. This very snippet of code:

tPlayers[i].OnTickContext = (void*)this;

assuming that OnTickContext is a bool variable, is equivalent to this:

tPlayers[i].OnTickContext = true;

Why is that?

First we're casting this pointer, which points to the object the method you are inspecting is called on, to void*. Nothing too fancy here. The trick lies in assigning any pointer (including void*) to a bool variable. The convertion behaves as such - if the pointer was nullptr, the variable will be set to false. Otherwise, it will be set to true.

Clearly we see that assigning a pointer to a bool variable can either yield true or false, then why did I say that it's always true in this context?

That's because of the nature of this pointer. The this pointer is a pointer to the object that you are calling a method on. You cannot call a method without an object. The this pointer will never be nullptr.

To summarise, neither the cast ((void*)) or the assignment of the pointer is necessary at all. Some compilers may even warn you that the assignment will always yield a true value.

Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • 4
    To be blunt I think it's equally likely that the OP is incorrect when he said that `OnTickContext` is a `bool`. Another possibility is that `OnTickContext` was `void*` when the above code was written but was then incompetently changed to be a `bool`. – john Feb 09 '19 at 09:48
  • @john that is very likely to be the case, I admit. – Fureeish Feb 09 '19 at 09:50
  • 1
    `OnTickContext` is very likely a pointer to an event handler, not a `bool`. – Ted Lyngmo Feb 09 '19 at 09:52
  • @TedLyngmo more likely to be a parameter passed to an event handler? – john Feb 09 '19 at 09:54
  • Could be - but I get the feeling that it's the C++ Builder closure style. – Ted Lyngmo Feb 09 '19 at 09:55
  • But it depends on what `bool` actually means. In some cases of legacy we can see comething alike `typedef unsigned char BOOL`. In that case, value may be just truncated to lower byte of target, that may effectively be 0x00 even when pointer itself is not NULL. IMHO construction `something = !!(void *)pointer` looks safer and more descriptive. – Serge Ageyev Feb 09 '19 at 10:07
-2

The programmer made a class in which he made OnTickContest bool variable which should be in public section. Then, he made an array of that class and assigns the value of that bool variable to the address of the calling object(this keyword here points to). This means if variable array has address it will be 1(true) else 0(false).

Abhi38
  • 307
  • 1
  • 2
  • 6