Solved

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

Posted on 2001-08-26
5
663 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
> 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now