Convert to char * in function call : conversion Function « Class « C++






Convert to char * in function call

Convert to char * in function call
#include <iostream>
#include <cstring>
using namespace std;

class StringClass {
  char str[80];
  int len;
public:
  StringClass(char *s) { 
     strcpy(str, s); 
     len = strlen(s); 
  }
  operator char *() {  // convert to char*
     return str; 
  }
};

int main()
{
  StringClass s("This is a test\n");
  char *p, s2[80];

  p = s;              // convert to char *
  cout << "Here is string: " << p << '\n';

  
  strcpy(s2, s);
  cout << "Here is copy of string: " << s2 << '\n';

  return 0;
}


           
       








Related examples in the same category

1.A simple conversion function example.A simple conversion function example.