-
March 9th, 2013, 04:55 AM
#1
Friend class and pointer function
Hi! I need help with understanding this block of code, particullary this line: *getLeftChild() { return this - _child; }
Code:
public class UpperNode
{
BOX _box;
int _child;
FORCEINLINE UpperNode *getLeftChild() { return this - _child; }
...
};
Here I have this function:
Code:
void
UpperNode::visulization(int level)
{
if (isLeaf())
_box.visulization();
else
if ((level > 0)) {
if (level == 1)
_box.visulization();
else
if (getLeftChild())
getLeftChild()->visulization(level-1);
if (getRightChild())
getRightChild()->visulization(level-1);
}
}
It also makes calls for "getLeftChild()";
But I see that getLeftChild expects function pointer, and I absolutely have no clue where "this" comes from inside function body.
(return this - _child) - "this" has to be integer.
Or, if we gave pointer, and "this" is reffering to some UpperNode, then I can't understand to which one, I have no UpperNode array defined or something. So if this functions is actually scaling pointer address, then scaling where to? I could comprehend it, if I had some array of UpperNodes, but not just class. I have UpperNodes array defined in other friendly class, but don't think they are related
Last edited by RyuMaster; March 9th, 2013 at 05:30 AM.
-
March 9th, 2013, 06:46 AM
#2
Re: Friend class and pointer function
This article should help to explain 'this'
http://www.learncpp.com/cpp-tutorial...-this-pointer/
'this' is a pointer to the class object the class function is working with. The 'this' pointer is a hidden pointer inside every class member function that points to the class object the member function is working with.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 9th, 2013, 09:33 AM
#3
Re: Friend class and pointer function
Sure, but that code is indexing this, which really is rather funky and, as the OP altrady suspected, only works in the context of an array of such objects. This is what the OP apparently (and quite understandably) seems to have problems to comprehend. I'm afraid that can't be explained without seeing the code in a larger context, though. (And I don't think it's directly reated to friend classes in any way at all.)
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|