Click to See Complete Forum and Search --> : CString forward declaration in .NET


tbrammer
July 22nd, 2003, 01:04 PM
Hi there

In C++ Headers I often use a forward declaration for CString like

class CString;
int someStringRoutine(CString &ref);
int otherStringRoutine(CString *ptr);

just to make sure, that the header also works with sources, that do not #include "stdafx.h".
Now I have to port a huge project to .NET, where CString is not a class anymore - but a

typedef ATL::CStringT< TCHAR, StrTraitMFC< TCHAR > > CString;

How can I do a forward declaration for this CString version?

-- Thomas

digitalasp
July 22nd, 2003, 01:37 PM
Originally posted by tbrammer
Hi there

In C++ Headers I often use a forward declaration for CString like

class CString;
int someStringRoutine(CString &ref);
int otherStringRoutine(CString *ptr);

just to make sure, that the header also works with sources, that do not #include "stdafx.h".
Now I have to port a huge project to .NET, where CString is not a class anymore - but a

typedef ATL::CStringT< TCHAR, StrTraitMFC< TCHAR > > CString;

How can I do a forward declaration for this CString version?

-- Thomas

Unfortunately there's no easy way around this unless you include the required header files for everything that sees CString. MS probably should have subclassed CStringT and implemented the constructors simply for portability sake.....but I'm sure that was a rational design decision behind the typedef usage :D

Your may want to consider doing something like the following:

class CXString : public CString {
// Declare/implement constructors here

};

Then replace all occurances in your code to CString with CXString. This will afford you the ability to declare forward references when necessary.

I do this with all my projects for CString, CArray, CList, etc. simply for the fact that I have extended all of them with additional functionality (i.e. CString has Replace(), LoadFrom(), Split(), and several other methods added to it).


Unfortunately I don't know what type of an impact any of the changes I could suggest would have since I don't know the size of your codebase, the type of project, or the reason behind now including stdafx.h in all files but you're bound to get a suggestion that will fit exactly what you need.


...Chet...