Clarification about this

Say if i have a function like:

1
2
3
4
5
6
void SceneNode::attachChild(Ptr child) {     

child->mParent = this;     
mChildren.push_back(std::move(child)); 

}


Does this means that the "this" keyword is the function itself or the whole class itself?

In some blog I found this one

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.


So "this" is the member function? Im a little confuse. I thought it was the whole class?

So "this" is the member function?
No, it is an implicit parameter which can be used in class member functions. Internally all member function looks like:
1
2
3
4
5
6
7
class foo
{
    int doSomething(double d);

//...
//Internally:
int foo@doSomething(foo* this, double d)


Also there something that makes me wonder: what is the type of Ptr in your code?
Last edited on
Ow thats was a typedef of smart pointer.

So i am doing a recursion here. I am passing the class to its own variable?

I do not see recursion in your code, and it looks like sensible code: you are setting pointer to the parent in passed child to yourself and store it as own child.
If mParent is non-owning pointer then everything should be all right.
Topic archived. No new replies allowed.