Serving the Quantitative Finance Community

 
User avatar
babu12
Topic Author
Posts: 0
Joined: January 30th, 2008, 2:47 pm

this pointer in C++

June 19th, 2009, 9:57 am

hi, i am cofused with the this pointer in C++ can someone please help. my understanding is the the "this" pointer contains the address the object of a class. but in most of the cases of function overloadinng we come across situations like1> X& X::memberfunctionofX()2> {3> // code here4> return *this;5> } where X is a class. notice memberfunction of X should return a reference of type X (line 1 of code). but line 4 of code returns *this. now doesn't *this dereference the "this" pointer ? and hence the reference is not returned. maybe my understanding is not correct, someone please help. thanks in advance.
 
User avatar
samyonez
Posts: 0
Joined: October 7th, 2004, 10:01 am

this pointer in C++

June 19th, 2009, 2:24 pm

Compare with the case where you return a reference from a global function. at which of the following does you understanding give way?// a function returning a pointer to an objectMyClass* func1(){ MyClass* p(new MyClass); return p;}// a function returning a reference to an objectMyClass& func2(){ MyClass* p(new MyClass); return *p;}// a member function returning a pointer to the objectMyClass* MyClass::memFunc1(){ MyClass* p(this); return p;}// a member function returning a reference to the objectMyClass& MyClass::memFunc2(){ MyClass* p(this); return *p; // obviously same as "return *this"}
 
User avatar
CommodityQuant
Posts: 57
Joined: July 5th, 2007, 6:16 am

this pointer in C++

June 21st, 2009, 10:13 am

To the OP:You are correct that *this deferences the this variable to fetch an object of type XThere are now two possibilities: 1) A copy of the type X object can be returned. In this case, your code would change slightly. Line 1 would read X X::memberfunctionofX()2) The actual type X object can be returned with no copying. That is the case in the code you wrote. "Returning an actual object without copying" is just a non-jargon way of saying "returning a reference".You are also correct that the code you present is more common than writing X X::memberfunctionofX() One reason is that, in your code you can write stuff likex.memberfunctionofX() = ...; You can't do this without using a reference.It is unclear (to me) why you use the term "overloading" in your post. This concept does not appear anywhere in your code. Overloading means to use the same function name for two different functions. For exampleint Overloading(int x, int y){ return x + y;}int Overloading(int x){return x+1;}This is perfectly legal code despite giving the same name to two totally different functions, and this illustrates what "overloading" means. So I don't know why you mention "overloading" in your post.CommodityQuant
 
User avatar
CommodityQuant
Posts: 57
Joined: July 5th, 2007, 6:16 am

this pointer in C++

June 21st, 2009, 6:36 pm

Originally posted by: samyonezCompare with the case where you return a reference from a global function. at which of the following does you understanding give way?snipQuote// a function returning a reference to an objectMyClass& func2(){ MyClass* p(new MyClass); return *p;}I think this is code produces a runtime error because it returns a reference to a local variable.A fix could be:MyClass& func2(){ MyClass* p(new MyClass); static MyClass x = *p; return x;}CommodityQuant
 
User avatar
Cuchulainn
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

this pointer in C++

June 21st, 2009, 7:03 pm

Quote// a function returning a reference to an objectMyClass& func2(){ MyClass* p(new MyClass); return *p;}A fix could be:MyClass& func2(){ MyClass* p(new MyClass); static MyClass x = *p; return x;}Achtung!!I think you have a memory leak here; scoped pointer might be safer.Plan B: If this function is a member function (!) you could create a redundant member data and just return it each time.Static objects are in static memory which is not OK because you have no control over its creation and it screws up multi-threads.. ==> each thread has to make a copy.Another solution is to *redesign* the function. It is very seldom you need to use statics, they always remind me of COBOL global data.
Last edited by Cuchulainn on June 20th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
CommodityQuant
Posts: 57
Joined: July 5th, 2007, 6:16 am

this pointer in C++

June 22nd, 2009, 9:19 am

Thanks. Was particularly interested in your comments on multithreading. I'm struggling a bit to find a good guide to c++ multithreading on the web. Do you have any recommendations?CommodityQuant
 
User avatar
Cuchulainn
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

this pointer in C++

June 22nd, 2009, 10:03 am

QuoteOriginally posted by: CommodityQuantThanks. Was particularly interested in your comments on multithreading. I'm struggling a bit to find a good guide to c++ multithreading on the web. Do you have any recommendations?CommodityQuantA good start is to buy books on OpenMP and look at forums on www.openmp.org. There's web stuff but is not gratis, necessarily.here are some other sites http://www.datasimfinancial.com/forum/v ... .php?t=202
Last edited by Cuchulainn on June 21st, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
samyonez
Posts: 0
Joined: October 7th, 2004, 10:01 am

this pointer in C++

June 22nd, 2009, 2:24 pm

CommodityQuant; my func2() didn't return a ref to local (stack) variable, it returns a ref to a heap variable, on whose address you will need to call delete if you are to avoid mem leaks. Your func2() alreadys contains a mem leak though; your static variable will be assigned (actually initialised) only the first time round, every subsequent call to func2() will "new" a MyClass on the heap which you can never recover.
 
User avatar
CommodityQuant
Posts: 57
Joined: July 5th, 2007, 6:16 am

this pointer in C++

June 22nd, 2009, 5:37 pm

Agreed, my mistake.CommodityQuant
 
User avatar
Cuchulainn
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

this pointer in C++

June 22nd, 2009, 5:51 pm

QuoteOriginally posted by: samyonezCommodityQuant; my func2() didn't return a ref to local (stack) variable, it returns a ref to a heap variable, on whose address you will need to call delete if you are to avoid mem leaks. Your func2() alreadys contains a mem leak though; your static variable will be assigned (actually initialised) only the first time round, every subsequent call to func2() will "new" a MyClass on the heap which you can never recover.Indeed, it is not Java.
 
User avatar
dirtydroog2
Posts: 0
Joined: June 12th, 2009, 2:56 pm

this pointer in C++

June 23rd, 2009, 4:03 pm

QuoteOriginally posted by: babu12hi, i am cofused with the this pointer in C++ can someone please help. my understanding is the the "this" pointer contains the address the object of a class. but in most of the cases of function overloadinng we come across situations like1> X& X::memberfunctionofX()2> {3> // code here4> return *this;5> } where X is a class. notice memberfunction of X should return a reference of type X (line 1 of code). but line 4 of code returns *this. now doesn't *this dereference the "this" pointer ? and hence the reference is not returned. maybe my understanding is not correct, someone please help. thanks in advance.you could return a X*& if you wanted!
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

this pointer in C++

June 23rd, 2009, 5:26 pm

I think babu12 is simply misunderstanding the syntax of references (and possibly the difference between references and pointers):X *p1= new X();// This works fineX &r1= *p1;I.e. when initialising a reference one does not need to supply a pointer, but rather an object (technically, a l-value)