Casting this Vector3 into a const char* has worked in implicit and explicit conversions, but hasn't when attempting to convert it at construction time. Only then does const char* 'v3pchar' return blank. Any thoughts? The full code is below. Thanks!
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
struct Vector3 {
int x, y, z = 0;
Vector3() {
cout << "Vector3()" << endl;
};
Vector3(int X, int Y, int Z) : x(X), y(Y), z(Z) {
cout << "Vector3(int X, int Y, int Z)" << endl;
};
// Focal point here
operator const char* () {
cout << "operator const char* called" << endl;
v3string = to_string(x) + ", " + to_string(y) + ", " + to_string(z);
v3pchar = v3string.c_str();
return v3pchar;
}
private:
string v3string = "Never even blank";
const char* v3pchar = "So why does this become blank?";
};
int main() {
Vector3 v1 = Vector3(1, 2, 3);
cout << "Vector3 implicit cast to char*: [" << v1 << "]" << endl; // Works
const char* v2 = v1;
printf("Vector3 explicit cast to char*: [%s]\n", (const char*)v2); // Works
cout << "---------------------------------------------" << endl;
const char* v3 = Vector3(4, 5, 6);
cout << "Vector3 instantiation cast to char*: [" << v3 << "]" << endl; // Empty
return 0;
};