Skip to content

Instantly share code, notes, and snippets.

@eatnumber1
Last active December 19, 2015 08:09
Show Gist options
  • Save eatnumber1/5923212 to your computer and use it in GitHub Desktop.
Save eatnumber1/5923212 to your computer and use it in GitHub Desktop.
An example of user-defined implicit type conversion.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class string {
public:
string( const char *str ) : m_str(strdup(str)) {}
string( int i ) {
asprintf(&m_str, "%d", i);
}
~string() {
free(m_str);
}
const char *c_str() {
return m_str;
}
private:
char *m_str;
};
class B {
public:
void doNothing( string str ) {
printf("%s\n", str.c_str());
}
};
int main() {
B b;
b.doNothing(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment