cast operator

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
 
C

Carl Ribbegaardh

How about writing a constructor that takes a char[] ?
Then the char[] would be converted into a String, and all you need would be
an operator + for 2 Strings. :)

....i hope.

/Carl
 
R

Ron Natalie

Ali R. said:
Hi guys,

I have a string class, which is a wrapper for a char *
with a few operators, like +

Sounds like what you want is a converting constructor, like the std::string
class has.

class String {
public:
String(const char*);
String();
....
};

Then you pretty much give a const char* to anything expecting a String and it will
be converted.

The operator char*() you defined does the opposite (and is probably ill-advised).
It converts you String class to a const char*.
 
J

Jonathan Turkanis

Ali R. said:
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);

This is the one you can get rid of easily, by providing a non-explicit
String constructor taking a const char*. That's how the standard
library does it:

String(const char*);

<snip>
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;
}

This should work. There are some well-known problems that can arise
with conversion operators, which you can find discussed in books like
Effective C++, Modern C++ Design, ... (and maybe also the FAQ for this
group.) This is why the standard library uses converting constructors
but explicit member functions (c_str() and data()) for converting to
const char*.
What I get is an error on the cout line saying

Thanks Ali

I wish my compiler knew my name, and was so polite!

Jonathan
 
A

Ali R.

Jonathan Turkanis said:
This is the one you can get rid of easily, by providing a non-explicit
String constructor taking a const char*. That's how the standard
library does it:

String(const char*);



This should work. There are some well-known problems that can arise
with conversion operators, which you can find discussed in books like
Effective C++, Modern C++ Design, ... (and maybe also the FAQ for this
group.) This is why the standard library uses converting constructors
but explicit member functions (c_str() and data()) for converting to
const char*.
LOL, I fogot to cut and paste the error line! And I can't type today
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top