Hi All,

I was writing a code. Accidently I came across a situation where I assigned string variable to a user- defined class. And strange thing is that string was filled with proper value.

Please see the code below:-

#include<iostream>
using namespace std;

class JStringToCharConstStar
{
public:
// constructor / destructor
JStringToCharConstStar (int a,
std::string j_string);
~JStringToCharConstStar (void);

// accessor
operator char const * (void) const
{
return(m_string);
}

private:
// intentionally unimplemented
JStringToCharConstStar (JStringToCharConstStar const &);
JStringToCharConstStar & operator = (JStringToCharConstStar const &);

// private data members
int abc;
std::string m_j_string;
char const * m_string;
}; // class JStringToCharConstStar


JStringToCharConstStar::
JStringToCharConstStar (int a,
std::string j_string)
:abc(a),
m_j_string(j_string),
m_string(NULL)
{
m_string = m_j_string.c_str();
if (NULL == m_string)
{
cout<<"GetStringUTFChars failed.\n";
}
}

JStringToCharConstStar::
~JStringToCharConstStar (void)
{
if (NULL != m_string)
{
cout<<"Destructor called";
}
}

int main()
{
std::string a;
char const *k = "vinit";
a = k;
cout<<"a="<<a<<"\n";
JStringToCharConstStar jstring(5,a);
std::string b;
b = jstring;
cout<<"b="<<b;
return 0;
}


output is
a=vinit
b=vinit


Question :- How come this is possible. I have assigned string b to object of class JStringToCharConstStar .
I did overloaded char const * to use it but it seems that I donot need that.

Can someone please explain me that is the issue here and if possible point me to some tutorial.

Thanks in advance for your help.

Thanks
Vinit