I have a class: .h
class test {
public:
int Length;
char* Name;
int* ARR;
test(int l, char* n, int* a);
test();
};
.cpp
test::test(int l, char* n, int* a){
Length=l;
Name=n;
ARR=a;
}
And main.cpp
#include<iostream>
void InAFunc(test *kio) {
int foo[3] = { 1,2,3 };
*kio = test(7, "Hello!", foo);
}
int main() {
test mmk;
InAFunc(&mmk);
std::cout << mmk.Name << mmk.ARR[1];
}
As I know, I may got an exception on ARR, because variable foo has been release at the end of function InAFunc. I need to do new or malloc to Arr to avoid it. My question is why "Hello!" is safe? I code a lot like this in the past but never wrong. Why leaving a string in constructor without new or malloc is OK? Variable Name in this class is a pointer.