Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-08-26
5
Medium Priority
?
710 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

722 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