Sunday, February 25, 2018

C++ Program to cast a class A object to a char * object.

#include <iostream>
#include <cstring>
using namespace std;
class A {
public: char *str;
    A(char *s) : str(s) { }

operator char*() {return str;}


};
int main() {
    char input[20];
    cin >> input;
    A a(input);
    // A ==> char *
    char *s = static_cast<char*>(a);
    strcat(s, "-success");
    cout << s;
    return 0;
}

Thanks
Happy Programming !

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows