Pratap Biswakarma
Pratap Biswakarma

Reputation: 117

what is the scope of this variable in c++?

If i define a function like this

std::string& returnstring(std::istringstream &ist)
{
    std::string temp;
    std::getline(ist,temp);
    return temp;
}

and if i use this temp which is passed by reference to another function like this

void displaystring(std::string &temp)
{
    std::cout<<temp;
}

would the string temp still be in scope when i pass it by reference to displaystring function?

Upvotes: 0

Views: 113

Answers (4)

Saurabh Dhage
Saurabh Dhage

Reputation: 1711

this is the reference variable which refers to the current class object which has called it. it has scope only inside the class and the same as the lifetime of the object which called it.

Upvotes: 0

Ganesh M S
Ganesh M S

Reputation: 1063

Nope since the temp variable is local to the returnstring function and once the code is out of the retuernstring function the temp variable is no longer valid. If at all you want to send the reference and get the answer then better option is to send the argument by reference as below,

void copyLineAsString(std::istringstream &ist, std::string& tempString)
{
    std::getline(ist,tempString);
    return;
}

I have few other suggestions to your code for the good coding practice, Never use the '&' for the return type / return a variable by reference. Use camel casing or _ separator for the function names(Ex returnString or return_string).

Upvotes: 1

Tomoko Sakurayama
Tomoko Sakurayama

Reputation: 51

No. The scope of temp is limited to the block where it is declared. Because you declared it on stack (you didn't use the keyword new), the variable temp will be destroyed the moment you exit the function. The only way to keep the variable would be to have the value of temp be copied to a variable outside the function. For example, this would work:

std::string returnstring(std::istringstream &ist)
{
    std::string temp;
    std::getline(ist,temp);
    return temp;
}

Because the return value is a copy rather than a reference, you could then do std::string returned_string = returnstring(ist); And have the value of temp be copied into a newly declared variable that could be used for your displaystring function.

In general, I would avoid declaring variables inside a function that you will need outside the scope of the function. Instead, return by value whenever you are returning variables built inside of the function (not recommended for large arrays and objects, however).

Upvotes: 1

eerorika
eerorika

Reputation: 238301

The scope of a variable is the scope where it is declared. std::string temp; is declared within the body of the function returnstring, therefore that function body is the scope of the variable. It is an automatic variable, and therefore its lifetime ends at the end of its scope and it is automatically destroyed.

The returned reference will be invalid (i.e. it is a dangling reference). If you attempt to access the non-existing object through the invalid reference, then the behaviour of the program will be undefined.

To fix the function, a simple solution is to return a value instead.

Upvotes: 3

Related Questions