Alexander Dong Back Kim
9/4/2008 11:31:00 AM
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