how to use Templates in VC++
How to use template for different fuctions with different parameteres?
example.......
convert(char inp[30],unsigned char source[30]);
unsig_2_char(char RegVolVal[30],unsigned char inp[30]);
char DllIntFunc1(char *inp);
void bstr2char(char inp[30],unsigned short source[30]);
can i make a temlpate for all these functions??
?
Re: how to use Templates in VC++
Question is: what do this function do?
Re: how to use Templates in VC++
In the win32 console object, the template can be used. But in the MFC or the others, I haven't had a test.
Re: how to use Templates in VC++
Quote:
Originally Posted by Q_leo
In the win32 console object, the template can be used. But in the MFC or the others, I haven't had a test.
There's no problem with the others as long as you are using a C++ compiler.
Re: how to use Templates in VC++
Not all the c++ compile support the standard of c++ language perfectly, include vc6.0 version as I known.
Re: how to use Templates in VC++
If you like to put it this way, even as of now, there is no compiler that is 100% comformance to the standard. Anyway, I like to emphasize that STL isn't only designed for console program. It is applicable in most situations, be it MFC, WTL, COM, etc.
Re: how to use Templates in VC++
Quote:
Originally Posted by Q_leo
In the win32 console object, the template can be used. But in the MFC or the others, I haven't had a test.
Well, then just take a look at MFC containers: CArray, CList, CStringList, etc. etc. Even CString in VC++ 7.x is a template class. Of course templates can be used with MFC.
Re: how to use Templates in VC++
Quote:
Originally Posted by cilu
Question is: what do this function do?
these functions conert 1 type of cahr array to another type such as
unsigned char to char
BSTR to char
Re: how to use Templates in VC++
You can't really do this using templates. Or at least not easily : or nicely.
For instance the code needed to change a LPCSTR to a LPWSTR (wide string array) is different for the code to change a LPCSTR to a BSTR.
Templates are used when you can replace something in the method verbatim : and this is not the case here.
Darwen.
Re: how to use Templates in VC++
Quote:
Originally Posted by vc185002
How to use template for different fuctions with different parameteres?
example.......
convert(char inp[30],unsigned char source[30]);
unsig_2_char(char RegVolVal[30],unsigned char inp[30]);
char DllIntFunc1(char *inp);
void bstr2char(char inp[30],unsigned short source[30]);
can i make a temlpate for all these functions??
?
Quote:
Originally Posted by darwen
You can't really do this using templates. Or at least not easily : or nicely.
For instance the code needed to change a LPCSTR to a LPWSTR (wide string array) is different for the code to change a LPCSTR to a BSTR.
Templates are used when you can replace something in the method verbatim : and this is not the case here.
Darwen.
I think typelist might also be what you are looking for, the thread in non-c++ is still there if you don't care to do a search.
"Typecast" is a completely different thing, in no connection with template.
Template, right on the top of my head is just another small special framework created for different types that are all abstractized to meet the demand of today's coders with generic code in the design process such that the clients or the users can freely play around with implemented modules !
Re: how to use Templates in VC++
Quote:
Originally Posted by Q_leo
In the win32 console object, the template can be used. But in the MFC or the others, I haven't had a test.
Quote:
Originally Posted by Kheun
If you like to put it this way, even as of now, there is no compiler that is 100% comformance to the standard. Anyway, I like to emphasize that STL isn't only designed for console program. It is applicable in most situations, be it MFC, WTL, COM, etc.
I am in a complete agreement with Kheun, and Q_Leo, you have to understand that MFC and some others, for instance, QT, Coral, ACE and more if you search the Internet, there is also even a debate on the classic MFC somewhere in non-VC++ forum, are generally called frameworks and are ussually used to create applications for, more of, bussiness purposes.
Template as my previous post mentioned is used for generic code and types, which then leads to the definition of generic programming.
Everything here is still on C++ related platforms and why the question or doubt as to whether or not it doesn't work on a mainly C++ FRAMEWORK ? :confused:
Le
Re: how to use Templates in VC++
Quote:
Originally Posted by vc185002
can i make a temlpate for all these functions??
Consider to use the Strategy pattern.
Re: how to use Templates in VC++
The strategy pattern assumes that all algorithms have the same interface, which in this case, they do not. I think simple function overloading would suffice here. Nothing complicated is really needed.
Re: how to use Templates in VC++
I honestly can't see any way of doing a general conversion routine for strings reliably and type-safely in C++.
I'd even forget function overloading and just have the conversion routines as static methods in a "CStringConverter" class e.g.
Code:
class CStringConverter
{
public:
static void Convert(BSTR bstrString, int nCodePage, CString &rsString);
static void Convert(LPCWSTR szString, int nCodePage, CString &rsString);
static void Convert(LPCSTR szString, int nCodePage, CComBSTR &rbstrString);
static void Convert(LPCWSTR szString, int nCodePage, CComBSTR &rbstrString);
// etc, etc
private:
CStringConverter() { } ;
} ;
Now to do this in C#, that's a different matter. You CAN do the conversion in C# generically.
But that's because C# is a true object oriented language, and C++ is not - i.e. in C# everything is an object, whereas in C++ this isn't the case.
Darwen.
Re: how to use Templates in VC++
Quote:
Originally Posted by darwen
I'd even forget function overloading and just have the conversion routines as static methods in a "CStringConverter" class...
That's still function overloading. Just at the class level.