Solved

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

Posted on 2001-08-26
5
673 Views
Last Modified: 2013-12-14
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 ??

0
Comment
Question by:sneeuw
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6426480
>> I'm thinking about using wstring as
>> basis (base clase) for my 'own' string
>> class with extra functionality
>> for my project.
That is probably a bad idea.  the basic_string class, along with most of the STL concrete classes (basically everything other than the stream classes), were not designed to be base classes.  While any class can be used a base class, its often (not always) awkward and messy if the class was not designed for it.

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 guess storing everything as Unicode
>> seems a good idea ..
probably.  obvously SBCS are not sufficient and MBCS are not very efficient.

>> wstring doesn't 'know' char* as input.
Nope. Only "wchar_t *"

>> I guess I need to call a Windows
>> routine to convert to Unicode
You can use the mbcstowcs() procedure which is standard C++.

>>  Do I need to convert all text I type-in
>> in my project ?  
You can do this like

MyString TestStr(L"This is a test") ;

where that "l" declares the text as a wide-character (long) string.

>> will e.g. MyString TestStr((wchar_t*)"This is a test") ; work ??

It will compile and then fail at run-time.  The cast converts the pointer, which is a pointer to a char, to be typed as a pointer to a wide char.  The data the pointer points to is unchanged and thus will cause a failure.

0
 
LVL 22

Expert Comment

by:nietod
ID: 6426481
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.
0
 

Author Comment

by:sneeuw
ID: 6426513
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 !!??)

0
 
LVL 22

Expert Comment

by:nietod
ID: 6426562
>> 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.)
0
 

Author Comment

by:sneeuw
ID: 6429155
> 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.
 
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question