C++ variable scope for references -


i'm writing c++ first time in long time , can't remember much. i'm struggling scope (along when use pointers vs references input parameters). specifically, if create on stack, how long remain there?

if have simple class like:

class person {     const std::string name_;  public:     person(const std::string& name) : name_(name) {}     const std::string& get_name() { return name_; } }; 

and have simple person generator method , main:

person* get_person() {     std::string name = "bob";     return new person(name); }  int main (int argc, char **argv) {     person* person = get_person();     // person's name bob here? or did bob go out of scope?     delete person; } 

before delete person, did person's name go out of scope?

i understand create new std::string , pass person, have 1 more variable cleanup. standard accept pointers or references in method signatures?

also, references tutorials on subject welcome.

you have no issues here. constructor of person copies value on stack.


Comments