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.
Re: how to use Templates in VC++
True Kevin. I was just clarifying.
Rats, stupid comment again ! Of course that was overloading... I really should read my posts before clicking the "add post" button. :wave:
Darwen.
Re: how to use Templates in VC++
:lol: It happens to all of us! :D
Re: how to use Templates in VC++
Quote:
Originally Posted by KevinHall
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.
I was sure someone will come to this conclusion.
Code:
struct ParamBase
{
virtual ~ParamBase();
};
struct CharParam: public ParamBase
{
char *buffer;
int size;
// whatever else needed
};
struct UCharParam: public ParamBase
{
unsigned char *buffer;
int size;
// whatever else needed
};
struct UShortParam: public ParamBase
{
unsigned short *buffer;
int size;
// whatever else needed
};
class StrategyConverter
{
public:
bool Convert(ParamBase* param1, ParamBase* param2);
};
class Char2UShortConverter
{
public:
bool Convert(ParamBase* param1, ParamBase* param2)
{
CharParam* p1 = (CharParam*)param1;
UShortParam* p2 = (UShortParam*)param2;
// perform the conversion
}
};
int main()
{
CharParam *param1 = new CharParam(...); // fill with appropriate data
UShortParam *param2 = new UShortParam(...); // fill with appropriate data
StrategyConverter *conv = new Char2UShortConverter();
conv->Convert(param1, param2);
delete param1;
delete param2;
delete conv;
return 0;
}
It's 3:30 AM here, so I hope I did not make any mistake...
Re: how to use Templates in VC++
Quote:
Originally Posted by darwen
You can't really do this using templates. Or at least not easily : or nicely.
That way of explaining don't you see is completely non-sense ?
I am not talking about word, or grammar processing but about logical reasoning instead. Sounds like a fortune teller.
Quote:
Originally Posted by darwen
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.
Darwen.
you already wrote a class with static void convert functions, now may i ask if you could post the code perhaps one of the functions you mentioned if possible to show their changes anyway ? :)
Quote:
Originally Posted by darwen
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.
Oh, Jesux Christ, the terms you use remind me of teh days when C# was first invented by MS to compete with Sun's java after a famous trial I guess all of us know well, surely also with the intention that it was going to be another OOL but have you ever wondered if it is really true or still untrue in some aspects ?True OOL, so fast is your conclusion i mean...
--Oh my (NO GOD)! <grabber>
Re: how to use Templates in VC++
Cilu,
I see what you're doing. However, you must admitt that unless there is some other compelling reason to create ParamBase and associated classes, that the heirachy and strategy pattern are overly complex to solve a problem that could otherwise be more easily solved with simple overloading. I mean, why complicate things with a class heirarchy, virtual function calls, and calls to new and delete? All this seems to add a lot of unnecessary overhead in my opinion.
- Kevin
Re: how to use Templates in VC++
Quote:
Originally Posted by KevinHall
I see what you're doing. However, you must admitt that unless there is some other compelling reason to create ParamBase and associated classes, that the heirachy and strategy pattern are overly complex to solve a problem that could otherwise be more easily solved with simple overloading. I mean, why complicate things with a class heirarchy, virtual function calls, and calls to new and delete? All this seems to add a lot of unnecessary overhead in my opinion.
I won't argue with that. I didn't say it's the best solution. I said it's a solution, that should be considered. And I've shown how it can be done. That's all.
Re: how to use Templates in VC++
:lol: Ok! :D
So, the conclusion of the matter is that simple overloading is the most straight-forward solution for this problem if taken in isolation. However, if there exists other related problems in your application, there are other possible solutions that may make sense to help tie the different concepts together. The strategy pattern is one of these possible solutions.
- Kevin
Remember, don't use a sledge-hammer to make a square peg fit into a round hole!