Link to home
Start Free TrialLog in
Avatar of sneeuw
sneeuwFlag for Belgium

asked on

wstring(char *Text) constructor ?? or how to handle ?

Hi,

I'm thinking about using wstring as basis (base clase) for my 'own' string class with extra functionality for my project.

The reasoning behind it is that I get text input in different ways.  Some of it is SBCS, some of it is MBCS and some of it is Unicode.
Still doubting about the best approach but I guess storing everything as Unicode seems a good idea ... ??

While doing so (playing with the concept) I noticed that wstring doesn't 'know' char* as input.
So ... I guess I need to call a Windows routine to convert to Unicode ... is this so ?  Any idea which one ?

Does that then also apply to e.g. :
MyString TestStr("This is a test") ;
I mean ... Do I need to convert all text I type-in in my project ?  
Or will e.g. MyString TestStr((wchar_t*)"This is a test") ; work ??

ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

By the way, the type of a string literal is tecnically a constant character (or wide character).  Not all compilers enforce this at this time, but they probably will in the future, so prepare for it now.  Thus che code should be

MyString TestStr(( const wchar_t*)"This is a test") ;

(Not that it fixes the problem, but it fixes one mistake.
Avatar of sneeuw

ASKER

It's a pitty wstring is not a good base class ...
I started creating a class based on it (only playing at the moment to find out it's potential before I start)

> There are usualy better alternatives than derivation when you want to create custom string classes.
> If you can explain what you want to "add" to the class, I might be able to suggest better alternatives.

I'm not entirely sure (yet) but ...
I wanted to have my own string class to once and for all get rid of all the difficulties I encounterd before and to have sometihing which can be ported to other C++ compilers (I don't like to work with VCL AnsiString in my 'deeper' code (in the GUI I don't mind)).

I wanted to define some unique types (typedef) so that the Mystring class would know how to treat the input
(e.g. byte swapped from joliet file-system tables, SBCS or Unicode (testing for what it is), output to : *char, wchar_t*, MBCS_t*

I also wanted to put some functions in there that gets rid of the last backslash, trailing blanks and so on ...

And I also wanted to include some code to load text from resource dlls (I don't think that is in the string class already !!??)

>> it's a pitty wstring is not a good base class ...
probably not.

The class has lots of features for "customization" through its template parameters.  Derivation usually makes little sense because the derived classes usually need a different (not larger, but different) interface, thus derivation is usually not the right choice.

It sounds, like you want to write your own string class.  It can use a wstring internally, as a data member, to record the string data.  But it would provide its own interface that allows the string to be assigned or copied from or to a variety of string types.  

(if you tried to do this with derivation you would quickly run into problems, not so much because of how basic_string is designed, but because you are not modeling a "is a" interface and thus derivation leads to an illogical interface.)
Avatar of sneeuw

ASKER

> but because you are not modeling a "is a" interface and thus derivation leads to an illogical interface

I'm not sure I understand ?

MyString is still a string ... right ?
so MyString "is a" string.