Solved

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

Posted on 2001-08-26
5
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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