ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wonder if anyone could help me with the following C++ question: I've been given a custom-made string class which handles string, wstring and bstr. It has a number of methods and assignment operators to convert to and from different types.
The app I work on compiles happily in VS6 and VS2008, but when trying to compile in Redhat (version 4.1.1 in Redhat 5.0), for example for the following code:
MYString yyy = "abc";
std::string xxx1;
xxx1 = yyy; // THIS LINE GIVES AN ERROR
string zzz1 = yyy; // THIS LINE WORKS FINE
Although, I have to wonder *why* you want this custom string class. The generally accepted sane approach is to use a UTF-8 encoded std::string everywhere. It's understandable if you want to use std::wstring everywhere and do encoding/decoding into UTF-8 on I/O as well. Having multiple string encodings flying around is usually a recipe for disaster.
>> Although, I have to wonder *why* you want this custom string class.
Indeed a good question. I dislike custom string-classes, but have to support this for multi-platform / UTF-8 support (VS6, VS2008 and Unix)
>> You need to define a conversion operator
>> operator std::string() const;
Tried this, to no help ;(
I had operator
operator const string&() const;
already defined. I suspect that's why the following works:
MYString yyy = "abc";
std::string zzz1 = yyy; // THIS LINE WORKS FINE
And I don't understand why then the following doesn't work:
MYString yyy = "abc";
std::string xxx1;
xxx1 = yyy; // THIS LINE GIVES AN ERROR
Maybe one option would be to simply define
#define MYString std::string in g++
(if std::string supports UTF8 encoding in g++), but still the above error has me baffled.
Hmm. You're only allowed one compiler conversion and one user-defined conversion, but a conversion to const std::string & should do it. Do you also have a const char* conversion? If so, the two will conflict. It's strange that the copy constructor works.. they should either both work or neither work.
Code:
Maybe one option would be to simply define
#define MYString std::string in g++
(if std::string supports UTF8 encoding in g++), but still the above error has me baffled.
All versions of std::string support UTF-8 encoding. Well, not quite all.. If you have for some insane reason a really old 7-bit char computer this code needs to support. And by "really old", I mean like PDP-11 old.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.