[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Casting from a string

Alexander Dong Back Kim

9/4/2008 9:16:00 AM

Dear members,

I'm trying to implement a complex number (not that complex =P) class
and make it compatible with string.
The following codes are brief case of what I'm trying to do...

class Complex
{
protected :
int _value;

operator string (void)
{
stringstream ss;
ss << this->_value;

string str;

if (ss >> str)
return str;
else
return "";
}

//? friend string::operator Complex (void);
};

As you can see the codes, the class can convert the value (which is
"int" in this case... I'm not talking about atoi or itoa here...).
However, I have no idea who I can overload from string to Complex
function...

Any suggestion or idea?

best regards,
Alex Kim
17 Answers

Obnoxious User

9/4/2008 9:30:00 AM

0

On Thu, 04 Sep 2008 02:16:06 -0700, Alexander Dong Back Kim wrote:

> Dear members,
>
> I'm trying to implement a complex number (not that complex =P) class and
> make it compatible with string.
> The following codes are brief case of what I'm trying to do...
>
> class Complex
> {
> protected :
> int _value;
>
> operator string (void)
> {
> stringstream ss;
> ss << this->_value;
>
> string str;
>
> if (ss >> str)
> return str;
> else
> return "";
> }
>
> //? friend string::operator Complex (void);
> };
>
> As you can see the codes, the class can convert the value (which is
> "int" in this case... I'm not talking about atoi or itoa here...).
> However, I have no idea who I can overload from string to Complex
> function...
>
> Any suggestion or idea?

Complex::Complex(std::string const &);
void Complex::operator=(std::string const &);

--
OU

Triple-DES

9/4/2008 10:11:00 AM

0

On 4 Sep, 11:16, Alexander Dong Back Kim <alexdb...@gmail.com> wrote:
> Dear members,
>
> I'm trying to implement a complex number (not that complex =P) class
> and make it compatible with string.
> The following codes are brief case of what I'm trying to do...
>
> class Complex
> {
> protected :
>     int _value;
>
>     operator string (void)
>     {
>         stringstream ss;
>         ss << this->_value;
>
>         string str;
>
>         if (ss >> str)
>             return str;
>         else
>             return "";
>     }
>
>     //? friend string::operator Complex (void);
>
> };
>
> As you can see the codes, the class can convert the value (which is
> "int" in this case... I'm not talking about atoi or itoa here...).
> However, I have no idea who I can overload from string to Complex
> function...
>
> Any suggestion or idea?
>

Do you mean conversion from string to complex?
In that case a constructor will do:

/* explicit */ Complex(const std::string& str)
{
// ...
}

Consider declaring this constructor explicit, or you may get some
unintended conversions from string to Complex.

DP

> best regards,
> Alex Kim

Alexander Dong Back Kim

9/4/2008 11:07:00 AM

0

On Sep 4, 8:10 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
> On 4 Sep, 11:16, Alexander Dong Back Kim <alexdb...@gmail.com> wrote:
>
>
>
> > Dear members,
>
> > I'm trying to implement a complex number (not that complex =P) class
> > and make it compatible with string.
> > The following codes are brief case of what I'm trying to do...
>
> > class Complex
> > {
> > protected :
> >     int _value;
>
> >     operator string (void)
> >     {
> >         stringstream ss;
> >         ss << this->_value;
>
> >         string str;
>
> >         if (ss >> str)
> >             return str;
> >         else
> >             return "";
> >     }
>
> >     //? friend string::operator Complex (void);
>
> > };
>
> > As you can see the codes, the class can convert the value (which is
> > "int" in this case... I'm not talking about atoi or itoa here...).
> > However, I have no idea who I can overload from string to Complex
> > function...
>
> > Any suggestion or idea?
>
> Do you mean conversion from string to complex?
> In that case a constructor will do:
>
> /* explicit */ Complex(const std::string& str)
> {
>   // ...
>
> }
>
> Consider declaring this constructor explicit, or you may get some
> unintended conversions from string to Complex.
>
> DP
>
> > best regards,
> > Alex Kim
>
>

Excellent!!!!!

Thanks DP and OU =)

Alexander Dong Back Kim

9/4/2008 11:10:00 AM

0

On Sep 4, 8:10 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
> On 4 Sep, 11:16, Alexander Dong Back Kim <alexdb...@gmail.com> wrote:
>
>
>
> > Dear members,
>
> > I'm trying to implement a complex number (not that complex =P) class
> > and make it compatible with string.
> > The following codes are brief case of what I'm trying to do...
>
> > class Complex
> > {
> > protected :
> >     int _value;
>
> >     operator string (void)
> >     {
> >         stringstream ss;
> >         ss << this->_value;
>
> >         string str;
>
> >         if (ss >> str)
> >             return str;
> >         else
> >             return "";
> >     }
>
> >     //? friend string::operator Complex (void);
>
> > };
>
> > As you can see the codes, the class can convert the value (which is
> > "int" in this case... I'm not talking about atoi or itoa here...).
> > However, I have no idea who I can overload from string to Complex
> > function...
>
> > Any suggestion or idea?
>
> Do you mean conversion from string to complex?
> In that case a constructor will do:
>
> /* explicit */ Complex(const std::string& str)
> {
>   // ...
>
> }
>
> Consider declaring this constructor explicit, or you may get some
> unintended conversions from string to Complex.
>
> DP
>
> > best regards,
> > Alex Kim
>
>

Dear Den,

Just one question. What do you mean by "unintended conversions"? Could
you explain me bit more detail like when it can happen and so on? I
appreciate that. Thank you very much.

regards,
Alex Kim

Alexander Dong Back Kim

9/4/2008 11:31:00 AM

0

On Sep 4, 9:10 pm, Alexander Dong Back Kim <alexdb...@gmail.com>
wrote:
> On Sep 4, 8:10 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
>
>
>
> > On 4 Sep, 11:16, Alexander Dong Back Kim <alexdb...@gmail.com> wrote:
>
> > > Dear members,
>
> > > I'm trying to implement a complex number (not that complex =P) class
> > > and make it compatible with string.
> > > The following codes are brief case of what I'm trying to do...
>
> > > class Complex
> > > {
> > > protected :
> > >     int _value;
>
> > >     operator string (void)
> > >     {
> > >         stringstream ss;
> > >         ss << this->_value;
>
> > >         string str;
>
> > >         if (ss >> str)
> > >             return str;
> > >         else
> > >             return "";
> > >     }
>
> > >     //? friend string::operator Complex (void);
>
> > > };
>
> > > As you can see the codes, the class can convert the value (which is
> > > "int" in this case... I'm not talking about atoi or itoa here...).
> > > However, I have no idea who I can overload from string to Complex
> > > function...
>
> > > Any suggestion or idea?
>
> > Do you mean conversion from string to complex?
> > In that case a constructor will do:
>
> > /* explicit */ Complex(const std::string& str)
> > {
> >   // ...
>
> > }
>
> > Consider declaring this constructor explicit, or you may get some
> > unintended conversions from string to Complex.
>
> > DP
>
> > > best regards,
> > > Alex Kim
>
> Dear Den,
>
> Just one question. What do you mean by "unintended conversions"? Could
> you explain me bit more detail like when it can happen and so on? I
> appreciate that. Thank you very much.
>
> regards,
> Alex Kim

Dear members,

As Den and Obnoxious suggested, I added an operator overloading in the
complex class.

class Complex
{
protected :
int _value;

Complex();
~Complex();
Complex(string const & str);
operator=(string const & str);
operator=(int32_t v);

operator string (void)
{
stringstream ss;
ss << this->_value;

string str;

if (ss >> str)
return str;
else
return "";
}


};

Now I added another operator "operator=(int32_t v);". I'm using visual
c++ 6.0 and it now complains about ambigous declaration when I use the
class like the following.

Complex a, b, c;
a = "100"; // Compiler doesn't complain and works fine!
b = 10; // This is also works fine!!!
// BUT!!!
c = 0; // THIS IS THE PROBLEM LINK!!!

Why "c = 0;" creates the error message? Is it because the compiler
doesn't know whether "0" is integer or NULL or even boolean?? Is there
any way to trick compiler accept this "0" as integer "0"?

best regards,
Alex Kim

James Kanze

9/4/2008 3:16:00 PM

0

On Sep 4, 11:16 am, Alexander Dong Back Kim <alexdb...@gmail.com>
wrote:

> I'm trying to implement a complex number (not that complex =P)
> class and make it compatible with string.

What do you mean by compatible here? Complex numbers and
strings are orthogonal, and should be completely independent of
one another.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thomas J. Gritzan

9/4/2008 10:20:00 PM

0

Alexander Dong Back Kim schrieb:
> As Den and Obnoxious suggested, I added an operator overloading in the
> complex class.
>
> class Complex
> {
> protected :
> int _value;
>
> Complex();
> ~Complex();
> Complex(string const & str);
> operator=(string const & str);
> operator=(int32_t v);

What return value type does your operator= functions have?

> operator string (void)
> {
> stringstream ss;
> ss << this->_value;
>
> string str;
>
> if (ss >> str)
> return str;
> else
> return "";

Why not simpler?
return ss.str();

> }
>
>
> };
>
> Now I added another operator "operator=(int32_t v);". I'm using visual
> c++ 6.0 and it now complains about ambigous declaration when I use the
> class like the following.

Who forces you to use the old Visual C++ 6.0?

> Complex a, b, c;
> a = "100"; // Compiler doesn't complain and works fine!
> b = 10; // This is also works fine!!!
> // BUT!!!
> c = 0; // THIS IS THE PROBLEM LINK!!!
>
> Why "c = 0;" creates the error message? Is it because the compiler
> doesn't know whether "0" is integer or NULL or even boolean?? Is there
> any way to trick compiler accept this "0" as integer "0"?

What is "the error message"? Error messages contain information which is
useful in finding errors. Don't you want to tell us the message?

--
Thomas

Alexander Dong Back Kim

9/5/2008 1:33:00 AM

0

On Sep 5, 1:15 am, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 4, 11:16 am, Alexander Dong Back Kim <alexdb...@gmail.com>
> wrote:
>
> > I'm trying to implement a complex number (not that complex =P)
> > class and make it compatible with string.
>
> What do you mean by compatible here? Complex numbers and
> strings are orthogonal, and should be completely independent of
> one another.
>
> --
> James Kanze (GABI Software) email:james.ka...@gmail.com
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Dear James,

First of all, it is honor to have your comment in this thread =)
The main reason I want to make the "complex" class cast-able with
std::string is I want to convert into a XML file format. For example,

If I have three instance of the class in a vector or some container
class...

vector<Complex> vComplex;
Complex a, b, c;
a = 10;
b = "100";
c = 0;

vComplex.push_back(a);
vComplex.push_back(b);
vComplex.push_back(c);

SaveToXML(vComplex); // Saving function that generates an XML file

....

I expect the XML look something like...

<Container>
<Complex> "10" </Complex>
<Complex> "100" </Complex>
<Compelx> "0" </Complex>
</Container>

In order to do this, in my opinion, std::string compatibility would be
the thing really nice to have for further string-related operations.
I'm sorry if my explanation is not clear enough. If you think my
approach is wrong or you think there are other approaches for doing
this kind of thing, please feel absolutely free to tell and let me
know about them. =)

regards,
Alex Kim

Alexander Dong Back Kim

9/5/2008 1:37:00 AM

0

On Sep 5, 8:20 am, "Thomas J. Gritzan" <phygon_antis...@gmx.de> wrote:
> Alexander Dong Back Kim schrieb:
>
> > As Den and Obnoxious suggested, I added an operator overloading in the
> > complex class.
>
> > class Complex
> > {
> > protected :
> > int _value;
>
> > Complex();
> > ~Complex();
> > Complex(string const & str);
> > operator=(string const & str);
> > operator=(int32_t v);
>
> What return value type does your operator= functions have?
>
I think I should've use "Complex & operator=" =) Thanks for that!

> > operator string (void)
> > {
> > stringstream ss;
> > ss << this->_value;
>
> > string str;
>
> > if (ss >> str)
> > return str;
> > else
> > return "";
>
> Why not simpler?
> return ss.str();
>

That's a good suggestion =)

> > }
>
> > };
>
> > Now I added another operator "operator=(int32_t v);". I'm using visual
> > c++ 6.0 and it now complains about ambigous declaration when I use the
> > class like the following.
>
> Who forces you to use the old Visual C++ 6.0?
>

Myself =) Since it seems you are wondering about my work, I'm just
playing around with Visual C++ 6.0 thesedays. When I solve this
problem then I will move on to Visual C++.NET... although I'm mainly
use GCC =)

> > Complex a, b, c;
> > a = "100"; // Compiler doesn't complain and works fine!
> > b = 10; // This is also works fine!!!
> > // BUT!!!
> > c = 0; // THIS IS THE PROBLEM LINK!!!
>
> > Why "c = 0;" creates the error message? Is it because the compiler
> > doesn't know whether "0" is integer or NULL or even boolean?? Is there
> > any way to trick compiler accept this "0" as integer "0"?
>
> What is "the error message"? Error messages contain information which is
> useful in finding errors. Don't you want to tell us the message?
>
> --
> Thomas

Thanks Thomas =)

Alexander Dong Back Kim

9/5/2008 1:38:00 AM

0

On Sep 5, 8:20 am, "Thomas J. Gritzan" <phygon_antis...@gmx.de> wrote:
> Alexander Dong Back Kim schrieb:
>
> > As Den and Obnoxious suggested, I added an operator overloading in the
> > complex class.
>
> > class Complex
> > {
> > protected :
> > int _value;
>
> > Complex();
> > ~Complex();
> > Complex(string const & str);
> > operator=(string const & str);
> > operator=(int32_t v);
>
> What return value type does your operator= functions have?
>
> > operator string (void)
> > {
> > stringstream ss;
> > ss << this->_value;
>
> > string str;
>
> > if (ss >> str)
> > return str;
> > else
> > return "";
>
> Why not simpler?
> return ss.str();
>
> > }
>
> > };
>
> > Now I added another operator "operator=(int32_t v);". I'm using visual
> > c++ 6.0 and it now complains about ambigous declaration when I use the
> > class like the following.
>
> Who forces you to use the old Visual C++ 6.0?
>
> > Complex a, b, c;
> > a = "100"; // Compiler doesn't complain and works fine!
> > b = 10; // This is also works fine!!!
> > // BUT!!!
> > c = 0; // THIS IS THE PROBLEM LINK!!!
>
> > Why "c = 0;" creates the error message? Is it because the compiler
> > doesn't know whether "0" is integer or NULL or even boolean?? Is there
> > any way to trick compiler accept this "0" as integer "0"?
>
> What is "the error message"? Error messages contain information which is
> useful in finding errors. Don't you want to tell us the message?
>
> --
> Thomas

Oh I missed a question. I'm sorry I can't remember the error code
number but it was somethign like "operator= ambiguous"

cheers,
Alex Kim