A
Ali R.
Hi guys,
I have a string class, which is a wrapper for a char *
with a few operators, like +
What I am trying to do is reduce the number of overloaded operators
so for insteance
class String
{
friend String operator + (const String &Left,const char *Right);
friend String operator + (const char *Left,const String &Right);
//It would be nice if I could avoid this
friend String operator + (const String &Left,const String &Right);
private:
char *m_Buffer;
}
It would be nice if I could avoid this
friend String operator + (const String &Left,const String &Right);
since String can be easily substituded with a char *
a line like this
String Str1("Test");
String Str2("Test");
String Res = Str1 + Str2;
should be able to call this version, if the compiler could automatically
cast the string object into a char *
friend String operator + (const String &Left,const char *Right);
is there a way I can define a cast operator for this class, like
class String
{
public
operator const char *() const { return m_Buffer; }
private:
char *m_Buffer;
}
so that I can do things like the + above, it would work?
I know I have seen this done somewhere, but I can figure out how they did
it.
What I get is an error on the cout line saying
Thanks Ali
I have a string class, which is a wrapper for a char *
with a few operators, like +
What I am trying to do is reduce the number of overloaded operators
so for insteance
class String
{
friend String operator + (const String &Left,const char *Right);
friend String operator + (const char *Left,const String &Right);
//It would be nice if I could avoid this
friend String operator + (const String &Left,const String &Right);
private:
char *m_Buffer;
}
It would be nice if I could avoid this
friend String operator + (const String &Left,const String &Right);
since String can be easily substituded with a char *
a line like this
String Str1("Test");
String Str2("Test");
String Res = Str1 + Str2;
should be able to call this version, if the compiler could automatically
cast the string object into a char *
friend String operator + (const String &Left,const char *Right);
is there a way I can define a cast operator for this class, like
class String
{
public
operator const char *() const { return m_Buffer; }
private:
char *m_Buffer;
}
so that I can do things like the + above, it would work?
I know I have seen this done somewhere, but I can figure out how they did
it.
What I get is an error on the cout line saying
Thanks Ali