G
GF
Hello,
I have a string class, but want to be able to assign it to some other
frameworks class:
I think I need to provide an global operator function, but I can't figure
out how to declare it.
// CODE
#include <string>
class mystring
{
public:
mystring():m_data(NULL)
{clear();}
mystring(const char* s)
{m_data=new char[strlen(s)+1]; strcpy(m_data, s);}
// copy ctor from std::string
mystring(const std::string& s)
{*this = s;}
// = operator from std::string
const mystring& operator=(const std::string& s)
{clear(); *this=mystring(s.c_str() ); return *this;}
void clear()
{delete[] m_data; m_data=new char[2]; strcpy(m_data, "");}
private:
char* m_data;
};
void test()
{
mystring ms;
std::string st;
ms = st; // fine!
st = ms; // fail
}
// /CODE
Many thanks,
-Gernot
I have a string class, but want to be able to assign it to some other
frameworks class:
I think I need to provide an global operator function, but I can't figure
out how to declare it.
// CODE
#include <string>
class mystring
{
public:
mystring():m_data(NULL)
{clear();}
mystring(const char* s)
{m_data=new char[strlen(s)+1]; strcpy(m_data, s);}
// copy ctor from std::string
mystring(const std::string& s)
{*this = s;}
// = operator from std::string
const mystring& operator=(const std::string& s)
{clear(); *this=mystring(s.c_str() ); return *this;}
void clear()
{delete[] m_data; m_data=new char[2]; strcpy(m_data, "");}
private:
char* m_data;
};
void test()
{
mystring ms;
std::string st;
ms = st; // fine!
st = ms; // fail
}
// /CODE
Many thanks,
-Gernot