I looked some code it says one of the reason to use "new" is to make a pointer not being destroyed after a function. For example
void input(string name, int age)
{
student *st = new student(name,age);
st->change();
}
int main(){
input("guagua",25);
if(!st){ cout<<"there is a pointer"<<endl;}
delete st;
return 0;
}
I have a simple class student whose head file looks like this, where change funtion just cout a sentence.
class student{
public:
student(string na, int ag);
void change();
private:
string name;
int age;
};
If new allocate a memory then I should be able to see "there is a pointer". However, this program cannot be compiled and the error is
‘st’ was not declared in this scope
How could I make st be visible after input function?