Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Making a std::string a member of a union ???

Reply
Thread Tools

Making a std::string a member of a union ???

 
 
Peter Olcott
Guest
Posts: n/a
 
      01-10-2007

"Jim Langston" <(E-Mail Removed)> wrote in message
news:9Q7ph.8$(E-Mail Removed)...
>> "Jim Langston" <(E-Mail Removed)> wrote in message
>> news:Hk%oh.284$(E-Mail Removed)...
>>> "Peter Olcott" <(E-Mail Removed)> wrote in message
>>> news5Uoh.22778$(E-Mail Removed)...
>>>>
>>>> "Ron Natalie" <(E-Mail Removed)> wrote in message
>>>> news:45a4062f$0$28093$(E-Mail Removed) m...
>>>>> Peter Olcott wrote:
>>>>>> "Ron Natalie" <(E-Mail Removed)> wrote in message
>>>>>> news:45a403f2$0$28100$(E-Mail Removed) m...
>>>>>>> Peter Olcott wrote:
>>>>>>>
>>>>>>>> If all that I want is std::string could I simply use [ std::string*
>>>>>>>> String ] in my union?
>>>>>>> You would need management code to allocate and deallocate it.
>>>>>>
>>>>>> I already figured that, are there any other issues?
>>>>> You need to remember which element you stored in the vector.
>>>>>
>>>>> Once you do all that you probably have reimplemented boost::any.
>>>>
>>>> Okay then this is the way that I will do it.
>>>
>>> It depends on what you are actually tryign to do. A union of a double and a
>>> pointer doesn't seem like it will do you any good. It sounds more like you
>>> want to refer to this double as either a double, or as a std::string,
>>> correct? It would seem a simple class would work for you. Something like:
>>>
>>> #include <iostream>
>>> #include <string>
>>> #include <sstream>
>>>
>>> class AnyType
>>> {
>>> public:
>>> operator double() const { return Val_; }
>>> operator std::string () const
>>> {
>>> std::stringstream Convert;
>>> Convert << Val_;
>>> return Convert.str();
>>> }
>>> AnyType* operator= ( const double Val ) { Val_ = Val; return this; }
>>> AnyType* operator= ( const std::string& Val )
>>> {
>>> std::stringstream Convert;
>>> Convert << Val;
>>> Convert >> Val_;
>>> return this;
>>> }
>>> private:
>>> double Val_;
>>> };
>>>
>>> int main()
>>> {
>>> AnyType MyDouble;
>>> MyDouble = 1234.56;
>>> std::cout << MyDouble << "\n";
>>> MyDouble = "2345.67";
>>> std::cout << MyDouble << "\n";
>>>
>>> std::string wait;
>>> std::getline( std::cin, wait );
>>> }
>>>
>>> This could easily be converted to a template for any type.
>>>
>>> Is this the kind of thing you are looking for?

>
> "Peter Olcott" <(E-Mail Removed)> wrote in message
> news:mZ5ph.1264$(E-Mail Removed)...
>>I can't tell what you are trying to do here, but, the std::string will
>>probably not hold data that can be converted to and from string.

>
> Please don't top post. Message rearranged.
>
> You mean not hold data that can be converted to and from a double?
>
> Okay, so you want your union to hold *either* a std::string or a double. There
> are a few ways to do this including templates, templates with common base
> class, pointers, etc...
>
> How is it you want to be able to use AnyType?
>
>
>

Its like I am making my own VARIANT record. I need some features that VARIANT
does not have.


 
Reply With Quote
 
 
 
 
Simon G Best
Guest
Posts: n/a
 
      01-10-2007
Peter Olcott wrote:
> "Simon G Best" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Peter Olcott wrote:
>>> "Simon G Best" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> What do you want a union for? Generally, unions shouldn't be used.
>>> I am creating my own computer language and I need a simple way to store the
>>> various elemental data types.

>> Sounds like you probably want runtime polymorphism, if I'm guessing right.
>> That means deriving from base classes, having virtual functions, and that sort
>> of thing. Much better than unions

>
> I don't think that it is a good fit for runtime polymorphism because I will have
> datatypes with disjoint sets of operations, such as std::string and double.
> runtime polymorphism is the best fit when the sets of operations are identical,
> yet their specific implementation varies. This is not one of those cases.


The sets of operations don't have to be identical for runtime
polymorphism to be appropriate. And, as you seem to be finding, unions
don't seem to be suitable for your needs, either.

Why do you need or want to use a union? What problem is the union
supposed to solve? Is it that you want to store your "various elemental
data types" in the same container as if they're all of the same type
when they're not? Is it because they're all "elemental data types"
while, at the same time, they're various, different "elemental data
types"? If so, then runtime polymorphism is most probably exactly what
you need, as that's what polymorphism is very much about - it's pretty
much what the word 'polymorphism' means!

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
 
Reply With Quote
 
 
 
 
Simon G Best
Guest
Posts: n/a
 
      01-10-2007
Peter Olcott wrote:
>
> Its like I am making my own VARIANT record. I need some features that VARIANT
> does not have.


Is that "VARIANT record" in the Pascal sense? If it is, then I really
do think you probably want runtime polymorphism, as that is the C++ way
of doing that kind of thing.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-10-2007

"Simon G Best" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Olcott wrote:
>> "Simon G Best" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Peter Olcott wrote:
>>>> "Simon G Best" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed)...
>>>>> What do you want a union for? Generally, unions shouldn't be used.
>>>> I am creating my own computer language and I need a simple way to store the
>>>> various elemental data types.
>>> Sounds like you probably want runtime polymorphism, if I'm guessing right.
>>> That means deriving from base classes, having virtual functions, and that
>>> sort of thing. Much better than unions

>>
>> I don't think that it is a good fit for runtime polymorphism because I will
>> have datatypes with disjoint sets of operations, such as std::string and
>> double. runtime polymorphism is the best fit when the sets of operations are
>> identical, yet their specific implementation varies. This is not one of those
>> cases.

>
> The sets of operations don't have to be identical for runtime polymorphism to
> be appropriate. And, as you seem to be finding, unions don't seem to be
> suitable for your needs, either.
>
> Why do you need or want to use a union? What problem is the union supposed to
> solve? Is it that you want to store your "various elemental data types" in
> the same container as if they're all of the same type when they're not? Is it
> because they're all "elemental data types" while, at the same time, they're
> various, different "elemental data types"? If so, then runtime polymorphism
> is most probably exactly what you need, as that's what polymorphism is very
> much about - it's pretty much what the word 'polymorphism' means!


I am creating my own custom computer language interpreter. The language itself
is based on "C" and will interface with C++ native code.

>
> --
> Simon G Best
> What happens if I mention Leader Kibo in my .signature?



 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-10-2007

"Simon G Best" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Olcott wrote:
> >
>> Its like I am making my own VARIANT record. I need some features that VARIANT
>> does not have.

>
> Is that "VARIANT record" in the Pascal sense? If it is, then I really do
> think you probably want runtime polymorphism, as that is the C++ way of doing
> that kind of thing.


One reason that I can't use run-time polymorphism is that the interpreted
language that I am constructing can not directly interface with polymorphic
class members. It will only have the capabilities of "C" and not C++.

>
> --
> Simon G Best
> What happens if I mention Leader Kibo in my .signature?



 
Reply With Quote
 
Simon G Best
Guest
Posts: n/a
 
      01-10-2007
Peter Olcott wrote:
> "Simon G Best" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>> Why do you need or want to use a union? What problem is the union supposed to
>> solve?

....
>
> I am creating my own custom computer language interpreter. The language itself
> is based on "C" and will interface with C++ native code.


That's far too vague and general. What's the /specific/ problem that
the union is supposed to solve? What is it /specifically/ that you're
trying to do?

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-10-2007

"Simon G Best" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Olcott wrote:
>> "Simon G Best" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>
>>> Why do you need or want to use a union? What problem is the union supposed
>>> to solve?

> ...
>>
>> I am creating my own custom computer language interpreter. The language
>> itself is based on "C" and will interface with C++ native code.

>
> That's far too vague and general. What's the /specific/ problem that the
> union is supposed to solve? What is it /specifically/ that you're trying to
> do?


I have a single set of memory locations that must be able to store data of a
fixed set of types. A language lacking the capability of C++ must interface with
this data. The data types must include all of the elemental types {char, int,
double} and one string type based on wide characters and something like a string
build from a std::vector of struct.

>
> --
> Simon G Best
> What happens if I mention Leader Kibo in my .signature?



 
Reply With Quote
 
Simon G Best
Guest
Posts: n/a
 
      01-10-2007
Peter Olcott wrote:
>
> I have a single set of memory locations that must be able to store data of a
> fixed set of types. A language lacking the capability of C++ must interface with
> this data. The data types must include all of the elemental types {char, int,
> double} and one string type based on wide characters and something like a string
> build from a std::vector of struct.


That's still too vague.

* What do you mean by "a single set of memory locations"? Is that from
the perspective of your interpreted language?

* When you say, "A language lacking the capability of C++ must
interface with this data", what do you mean by "interface"?

If you're trying to do what I /think/ you're trying to do, then I think
you're probably failing to properly separate your interpreted language
from your implementation of its interpreter. But as you're not at all
clear on what you're actually trying to do, I can only guess.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-10-2007

"Simon G Best" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Olcott wrote:
>>
>> I have a single set of memory locations that must be able to store data of a
>> fixed set of types. A language lacking the capability of C++ must interface
>> with this data. The data types must include all of the elemental types {char,
>> int, double} and one string type based on wide characters and something like
>> a string build from a std::vector of struct.

>
> That's still too vague.
>
> * What do you mean by "a single set of memory locations"? Is that from the
> perspective of your interpreted language?


It will be implemented as a std::vector<AnyType> Any;

>
> * When you say, "A language lacking the capability of C++ must interface with
> this data", what do you mean by "interface"?


Read and write based on subscript.

>
> If you're trying to do what I /think/ you're trying to do, then I think you're
> probably failing to properly separate your interpreted language from your
> implementation of its interpreter. But as you're not at all


I have no choice in this, the interpreted language is provided by a third party.
I am hooking this third party interpreted language into my system and then
exposing another different interpreted language implemented in terms of the
third party language.

> clear on what you're actually trying to do, I can only guess.
>
> --
> Simon G Best
> What happens if I mention Leader Kibo in my .signature?



 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-11-2007
"Peter Olcott" <(E-Mail Removed)> wrote in message
news:5Kbph.26449$(E-Mail Removed)...
>
> "Simon G Best" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Peter Olcott wrote:
>> >
>>> Its like I am making my own VARIANT record. I need some features that
>>> VARIANT does not have.

>>
>> Is that "VARIANT record" in the Pascal sense? If it is, then I really do
>> think you probably want runtime polymorphism, as that is the C++ way of
>> doing that kind of thing.

>
> One reason that I can't use run-time polymorphism is that the interpreted
> language that I am constructing can not directly interface with
> polymorphic class members. It will only have the capabilities of "C" and
> not C++.
>
>>
>> --
>> Simon G Best
>> What happens if I mention Leader Kibo in my .signature?


Okay, I think the easiest way would be to have a class that can store any of
the possible types of data. You'll need to overload operator= and operator
type for each type, then you'll want to store in this class what type is
actually being used. You'll have problems, however, when the type is
arbitary.

class AnyType
{
public:
operator std::string() { return StringVal; }
operator int() { return IntVal; }
// etc...
};

int main();
{
AnyType Foo;
// yada yada

std::cout << AnyType << "\n";
// ooops, what type is it supposed to output? std::string? int? float?
double? char? etc..
}

Assignments and constructors would be easier, because there will be a parm
that says what type it is.

AnyType Foo;
Foo = 12;
12 is an integer, and so would use operator=( const int ); no ambiguity
there.

AnyType Foo( 12.5 );
12.5 is a double, and so would use the constructor accepting double, so no
ambiguity there.

You will have problems when the type isn't known because of no parameter.
What is accepted in it's use?

std::cout << AnyType.val(INT) << "\n";
is something like that acceptable?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple union member initialization Ricky Lung C++ 5 08-19-2004 09:07 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Setting union member in structure Jeff Massung C++ 2 12-22-2003 05:53 PM



Advertisments