473,624 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conversion between string types

Hi,

I'm working with Borland C++ Builder 6.2.
My project uses the std::string class. However, Borland in
its infinite wisdom has its own string class, AnsiString.

To make my life easier, I want to create a function,
preferably a conversion operator, to convert from a
std::string to an AnsiString. Please note that I don't
want to change either std::string or AnsiString.

The Background
--------------
In using Borland's Database Engine (BDE), there is
a variant type "TVariant" which it uses for its
field types. The TVariant is a union:
class TVarRec

{
public:
union
{
Integer VInteger;
Boolean VBoolean;
Char VChar;
PExtended VExtended;
PShortString VString;
Pointer VPointer;
PChar VPChar;
TObject* VObject;
TClass VClass;
WideChar VWideChar;
PWideChar VPWideChar;
Pointer VAnsiString;
PCurrency VCurrency;

PVariant VVariant;
};
union
{
Byte VType;
};
};
I'm writing my own generic wrapper for fields,
and I have a function to fill in a TVarRec
variable:
class Field
{
public:
virtual void fill_variant(TV arRec * pv) const = 0;
};

class Integer_Field
: public Field
{
int value;
public:
virtual void fill_variant(TV arRec * p_variant) const
{*p_variant = value;}
};
So far, so good (I hope). Now I'm trying to make
a templated field class. The issue is that TVarRec
has no assignment for std::string, but for AnsiString.
Which means that I need a conversion for the
fill_variant method:
class String_Field
: public Field
{
std::string value;
public:
virtual void fill_variant(TV arRec * p_variant) const
{*p_variant = AnsiString(valu e.c_str());}
// The AnsiString type has constructor for C style strings.
};
When I convert String_Field to use a template, the content
of the fill_variant method differs from the Integer field.
template <typename Field_Value>
class Typed_Field
: public Field
{
Field_Value value;
public:
virtual void fill_variant(TV arRec * p_variant) const
{*p_variant = value;}
};
The thorn in this design is that Typed_Field<std ::string>
causes an error because there is no std::string type
defined in the TVarRec. The PShortString type will
convert from an AnsiString type though.

I have other cases where I am having to convert from
a std::string type to an AnsiString type (such as all
the windowing APIs). This is where a global conversion
function would come in handy. The function would be
really handy if it could be implicit so I don't have
to write something like:
edit_box.Text = AnsiStringFromS TLString(s);
I'd rather write:
string hello("hello");
edit_box.Text = hello; // Implied conversion

Any ideas?

BTW, this is not off-topic, as I am searching for
a conversion operator between types; regardless
of whether the types are STL, C++ runtime or
external libraries.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #1
2 3086
"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote...
My project uses the std::string class. However, Borland in
its infinite wisdom has its own string class, AnsiString.

To make my life easier, I want to create a function,
preferably a conversion operator, to convert from a
std::string to an AnsiString. Please note that I don't
want to change either std::string or AnsiString.
[...]
I have other cases where I am having to convert from
a std::string type to an AnsiString type (such as all
the windowing APIs). This is where a global conversion
function would come in handy. The function would be
really handy if it could be implicit so I don't have
to write something like:
edit_box.Text = AnsiStringFromS TLString(s);
I'd rather write:
string hello("hello");
edit_box.Text = hello; // Implied conversion

Any ideas?


An assignment operator cannot be anything else but a non-static
member. You already know that (hopefully). Type conversion
operators cannot be anything but non-static members. So, you
are pretty much screwed in that regard if you don't want to muck
with either class. What you did is basically the only path you
can take, I think.

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote...
My project uses the std::string class. However, Borland in
its infinite wisdom has its own string class, AnsiString.

To make my life easier, I want to create a function,
preferably a conversion operator, to convert from a
std::string to an AnsiString. Please note that I don't
want to change either std::string or AnsiString.
[...]
I have other cases where I am having to convert from
a std::string type to an AnsiString type (such as all
the windowing APIs). This is where a global conversion
function would come in handy. The function would be
really handy if it could be implicit so I don't have
to write something like:
edit_box.Text = AnsiStringFromS TLString(s);
I'd rather write:
string hello("hello");
edit_box.Text = hello; // Implied conversion

Any ideas?

An assignment operator cannot be anything else but a non-static
member. You already know that (hopefully). Type conversion
operators cannot be anything but non-static members. So, you
are pretty much screwed in that regard if you don't want to muck
with either class. What you did is basically the only path you
can take, I think.

Victor


Thanks Victor. I had a gut feeling that I was screwed.
I can create a template function and overloaded functions
to help me out:
void AssignToVarRec( const std::string& s,
TVarRec * p_varrec)
{
*p_varrec = AnsiString(s.c_ str());
return;
}

template <typename T>
void AssignToVarRec( const T& value,
TVarRec * p_varrec)
{
*p_varrec = value;
return;
}

So all I have to do is change my assignment operators
to use AssignToVarRec. A bit more complicated, but it
will work.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3026
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a templatized conversion operator defined as follows:
16
5116
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
4
6481
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent? Thanks in advance... -Nikhil
2
1657
by: Sankar Nemani | last post by:
Why does the following code throw an exception? object o = new object{"sankar", "sankar2"}; string s; s =(string)o; The C# language spec says something different on MSDN: For any two reference-types A and B, if an implicit reference conversion (Section 6.1.4) or explicit reference conversion (Section 6.2.3) exists from
3
3498
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust ------------------------------------------------------------------------
7
1224
by: karch | last post by:
Thanks to everyone who has helped with some of my elementary questions about C++/CLI equivalents to C#. I have a few more items that are giving me headaches. All help is appreciated. What is the proper way to convert the following? 1) data.ForEach(delegate(int num) { formatter->Serialize(stream, num); } ); (basically how are delegates implemented in C++/CLI)
9
2368
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New Derived(), New Derived(), New Derived()}
4
1824
by: mark.olszowka | last post by:
I am writing a generic property table as part of my application property_name property_type property_value All information is stored in the database as strings. In my application that maintains this data, I want to ensure that the user cannot type in a value that cannot be converted to the specified
8
5088
by: Nikola | last post by:
Hello, I'm writing a String class for C++ and I'm getting the following error message when using operator: test.cpp: In function ‘int main()’: test.cpp:7: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: string.h:19: note: candidate 1: char Types::String::operator(unsigned int) const
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.