How can I make an object be treated like a string (example inside)

I was looking over a custom string class (we'll call it String) the other day and I encountered the following feature:

1
2
3
4
5
6
7
void func(String s){
    cout<<"This works";
}
...
func("something something");
char* aWord = "something something";
func(aWord);


The code above works and I don't know why.
The function takes a String type object (not a std::string or char*), but works if you actually pass a char* or a compile-time string as an argument.

Could someone please explain how he did this?
I've looked through the source code but it's huge and I don't really know what to look for.
What makes this work for std::string is the non-explicit const char* constructor. I don't know of any other way to accomplish this so I guess it must be the same with your custom string class.

http://www.cplusplus.com/reference/string/string/string/
Last edited on
Topic archived. No new replies allowed.