Hi
all,
I apologize in
advance for the "newbie" taste of my question.
I was digging in the
market model code and found something of the form:
//////////////////////////////////////////////////////////////////////////////////
namespace QuantLib { class CMSwapCurveState; //! Drift computation for CMS market models /*! Returns the drift \f$ \mu \Delta t \f$.See Mark Joshi, <i>Rapid Computation of Drifts in a Reduced Factor Libor Market Model</i>, Wilmott Magazine, May 2003. */ class CMSMMDriftCalculator { public:... /////////////////////////////////////////////////////////////////////////// I found that in many other classes. Could someone
explain me very quickly what does this "class CMSwapCurveState" syntax do? If it's a way for the two classes to
interact with each other, why isn't the key word "friend"
used?
Furthermore, this is done before the class
definition, how does the compiler understand that they are
linked? If you could point
me to a C++ tutorial that explains this, since I failed to find any explanation
about this syntax.
Thanks,
Fabrice
__________________________________________________ ************** IMPORTANT MESSAGE ***************************** This e-mail message is intended only for the addressee(s) and contains information which may be confidential. If you are not the intended recipient please advise the sender by return email, do not use or disclose the contents, and delete the message and any attachments from your system. Unless specifically indicated, this email does not constitute formal advice or commitment by the sender or the Commonwealth Bank of Australia (ABN 48 123 123 124) or its subsidiaries. We can be contacted through our web site: commbank.com.au. If you no longer wish to receive commercial electronic messages from us, please reply to this e-mail by typing Unsubscribe in the subject line. ************************************************************** ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
On Fri, 2008-02-15 at 18:05 +1100, Lecuyer, Fabrice wrote:
> I was digging in the market model code and found something of the > form: > ////////////////////////////////////////////////////////////////////////////////// > namespace QuantLib { > > class CMSwapCurveState; > > class CMSMMDriftCalculator { > > public: > I found that in many other classes. > > Could someone explain me very quickly what does this "class > CMSwapCurveState" syntax do? Bonjour Fabrice, google for "C++ forward declaration". Luigi -- The young man knows the rules, but the old man knows the exceptions. -- O. W. Holmes ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
In reply to this post by Fabrice_CBA
Hi Fabrice, I haven't look at the code yet, but the declaration you see is called a forward declaration. Basically if a class, within a header file, contains methods or class types that are represented as pointers or references then in order to tell the compiler the makeup of these types you can either include the header files of these types or you can forward declare the class types. In the latter case you will need to include the needed header files within the cpp file of the class you are implementing. You will also need to add the needed header files to any other implementation file (cpp or maybe even hpp files if you implement methods inline) that includes this header file. The forward declare approach is a bit like, here's a type, but I'll tell you the makeup of it later on when it's actually used (within methods usually). You cannot used the forward declare methodology if you are actually passing the types explicitly (not a pointer or reference). The compiler needs to know the definition in this case. The forward declare approach really shortens the length of compilation (especially if the header files are edited frequency). Regards, Toyin Akin.
Messenger on the move. Text MSN to 63463 now! ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Thanks Toyin,
It all makes thanks now. I also googled "C++ Forward Declaration"
as advised by Luigi (thanks for that), and found a few more explanations that I
will share here with those interested.
- When the size or actual content of a
forward-casted classe is not important to the code, a forward declaration is
sufficient, the header file need not be included in the .h or in the
.cpp.
- Forward declaration can be used for cyclic dependency
between 2 classes.
- Forward declaration is to be prefered to header
inclusion when possible.
The last point makes me even more surprised not having heard of it
before. I guess I'll have a talk with my C++ teachers...
Thanks,
Fabrice
From: Toyin Akin [mailto:[hidden email]] Sent: Friday, 15 February 2008 7:21 PM To: Lecuyer, Fabrice; [hidden email] Subject: RE: [Quantlib-users] C++ Classes interaction Hi Fabrice, I haven't look at the code yet, but the declaration you see is called a forward declaration. Basically if a class, within a header file, contains methods or class types that are represented as pointers or references then in order to tell the compiler the makeup of these types you can either include the header files of these types or you can forward declare the class types. In the latter case you will need to include the needed header files within the cpp file of the class you are implementing. You will also need to add the needed header files to any other implementation file (cpp or maybe even hpp files if you implement methods inline) that includes this header file. The forward declare approach is a bit like, here's a type, but I'll tell you the makeup of it later on when it's actually used (within methods usually). You cannot used the forward declare methodology if you are actually passing the types explicitly (not a pointer or reference). The compiler needs to know the definition in this case. The forward declare approach really shortens the length of compilation (especially if the header files are edited frequency). Regards, Toyin Akin.
Messenger on the move. Text MSN to 63463 now! ************** IMPORTANT MESSAGE ***************************** This e-mail message is intended only for the addressee(s) and contains information which may be confidential. If you are not the intended recipient please advise the sender by return email, do not use or disclose the contents, and delete the message and any attachments from your system. Unless specifically indicated, this email does not constitute formal advice or commitment by the sender or the Commonwealth Bank of Australia (ABN 48 123 123 124) or its subsidiaries. We can be contacted through our web site: commbank.com.au. If you no longer wish to receive commercial electronic messages from us, please reply to this e-mail by typing Unsubscribe in the subject line. ************************************************************** ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Free forum by Nabble | Edit this page |