Serving the Quantitative Finance Community

 
User avatar
katmandu
Topic Author
Posts: 0
Joined: July 24th, 2002, 11:10 am

Pointers or References?

November 6th, 2002, 7:16 am

I have a little problem wiht classes in C++.1) I have to pass a class to a function2) I have to pass a class to a construcutor****************************************************************1) for the first case I declare my function like thisvoid function(MyClass &mc)....and I can pass descendant of MyClass without any problem********************************************************************2) when I have to pass a class to a constructor I cannot do the same, because the class could be destroyed AFTER the call to the constructor, for instance if it is the result of another function class AClass { AClass(MyClass &mc); } MyClass some_function(double a).... if I write AClass ac(some_function(5));I have the problem that the class is destroyed just AFTER the construcutor call and I cannot use it inside AClass methods. I cannot copy the class and use it as a non-reference variable because it could be a descendant and it could be very time-consumingI know how to solve it using pointers but I would like to know if there is a elegant methods using references...thanks
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

Pointers or References?

November 6th, 2002, 8:28 am

if you written MyClass correctly it has a copy constructor so just make AClass have a data member of typeMyClass and copy it into it. If you dont want to copy the MyClass object, then you could a reference counting wrapper class.MJ
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Pointers or References?

November 6th, 2002, 8:40 am

If it is your class, then you could presumably reference count it ?You could, for instance create a copy constructor that instead of copying, just incremented a count.
 
User avatar
katmandu
Topic Author
Posts: 0
Joined: July 24th, 2002, 11:10 am

Pointers or References?

November 6th, 2002, 11:07 am

you are right...I think I'll use a sort of Handle class I've found in Stroustroup book... but it is not so elegant as I wanted!I mean: can I find in the STL somethig useful?thanksbye
 
User avatar
apnea
Posts: 0
Joined: June 3rd, 2002, 9:13 am

Pointers or References?

November 6th, 2002, 12:19 pm

QuoteI mean: can I find in the STL somethig useful?The C++ Standard Library has auto_ptr and auto_ptr_ref, which can help you code the behaviour you want (they internalise reference counting). Be careful though...http://www.gotw.ca/publications/using_a ... tively.htm PS: Often when I encounter problems of this type, looking at the problem from a different angle made me realise that there was a better design...
 
User avatar
gotcpp
Posts: 0
Joined: July 14th, 2002, 3:00 am

Pointers or References?

November 6th, 2002, 1:46 pm

QuoteMyClass some_function(double a)....you are already copying your class in this statement. so I don't really see what is it you are trying to optimize the correct way would be to do MyClass& some_function(double a) in which case you would have the same problem in a different guise:if you created an object of MyClass as a local your reference is already invalid, if you created on the heap, you must be doing some kind of ref counting or something similar alreadylemme know if I make sense
Last edited by gotcpp on November 5th, 2002, 11:00 pm, edited 1 time in total.
 
User avatar
katmandu
Topic Author
Posts: 0
Joined: July 24th, 2002, 11:10 am

Pointers or References?

November 6th, 2002, 2:40 pm

you are right, I would like to return a referenceMyClass& some_function(double a)but I dont know where to create it. if I create in the heap I'll need some ref-count and to know who will destroy my object.so I found a Handle<class T> in Stroustroup book (actually it is the QuantLib implementation of it) which works well. It does internal ref count.so I pass always Handle<MyClass> instead of MyClass&...bye
 
User avatar
mholm

Pointers or References?

November 6th, 2002, 5:42 pm

If you are creating an object which is so important and used throughout your application, why do you delete it while the application is running? Create it once in the Application class or MainFrame class (assuming you are using MSVC). Then create a method which returns a reference to the object. As long as this object is in Mainfrm or the App classes you can get access to it from anywhere easily. You perform clean up when the application closes so there is no need to worry if something is using it when you delete it (assuming you used new etc). If its a static member its even easier.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

Pointers or References?

November 7th, 2002, 5:56 am

how about using the monostate pattern? I.e. make all the class data static, then you just copy it normally at zero cost but all copies of the object have the same state at all times.MJ
 
User avatar
katmandu
Topic Author
Posts: 0
Joined: July 24th, 2002, 11:10 am

Pointers or References?

November 7th, 2002, 7:54 am

Actually, I'm doing some matrix computation and I have to create and destroy matrices often in computationif I write A = B * C + Deach time an operator+ or operator* declared likeMatrix operator*(Matrix &, Matrix &) { Matrix result; .... fill the matrix .... return result; // I don't want to waste time copying this big object}is called it first creates a Matrix, fills it and then copy it on the return stantment, and so on.while I would it to return a reference (or pointer) to the Matrix created for the resultI think a good way to write it isHandle<Matrix> operator*(Handle<Matrix>, Handle<Matrix>..) { Handle<Matrix> result;.... fill the result .... return result; // it doesn't copy anything}do you agree?
 
User avatar
jens
Posts: 0
Joined: July 14th, 2002, 3:00 am

Pointers or References?

November 7th, 2002, 8:11 am

katmandu,if you really want to implement the handling of matrix temporaries from scratch, I suggest that you go toThe Object-Oriented Numerics Page.Follow the links to Linear Algebra and Array libraries (eg. MTL, MET, TNT, LinAlg, Newmat, Blitz) and see how they solved this (eg. ref counting, expression templates). Jens.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

Pointers or References?

November 7th, 2002, 9:53 am

Katmandu, i think auto_ptr was designed for doing what you are doing. There are other possible solutions. 1) redefine your syntax to be a functionvoid MatrixMult(matrix& result, const matrix& A, const matrix& B);then just write answer into result. This is clunky syntactically but efficient and easy. 2) make A*B+C return a new type that says it is equal to A*B+C but doesnt acutally evaluate anything. and make the operation D =new type, evaluate the expression and write the answer into D. There is discussion of this approach in Stroustrup. 3) there are actually optimizations in the C++ language which mean that if you put Matrix D(A*B+C) it may actually not need to copy the result into D. (see Sutter for this.)MJ
 
User avatar
trc
Posts: 4
Joined: April 4th, 2002, 2:28 pm

Pointers or References?

November 12th, 2002, 7:22 pm

Why not writeA=B; A*=C; A+=D;where the operators *=, += write into "this" and return "this" and also delete any workspace if necessary.
 
User avatar
Jim
Posts: 1
Joined: February 1st, 2002, 5:20 pm

Pointers or References?

November 13th, 2002, 1:24 pm

QuoteI think a good way to write it isHandle<Matrix> operator*(Handle<Matrix>, Handle<Matrix>..) {Handle<Matrix> result;.... fill the result ....return result; // it doesn't copy anything}do you agree? No, what needs reference counting in the matrix data, not the matrix itself. Have the Matrix class contain a Handle<MatrixData>. Then you can still use references to the Matrix class and avoid the coupling that the Handle<> template introduces. Copy construction and assignment is very efficient. In addition, you can have various subclasses of MatrixData (like IdentityMatrixData, TridiagonalMatrixData, ...) which know how to add/multiply/... much more efficiently given the known structure of the data. Also, I'd implement MatrixData and its subclasses as templates in case the data is integers and not doubles.