0

How can we use const std::string variable defined in one function to be used in another function of the same program.

int sample::convert()
{
    fun()
     {
        //returns string;
     }
    const std::string sender = fun()
}

void sample::write()
{
   //I want to use sender variable here like below
   std::string var;
   var = sender;

}
  • 1
    The problem is that the `sender` variable is a temporary one. You cant use it outside of the `convert()` function. – vahancho Jul 14 '20 at 13:58
  • @vahancho: You also couldn't reach it (in a portable way) if it had `static` storage duration. – Bathsheba Jul 14 '20 at 13:59
  • 1
    Note also that `sender` being `const` isn't relevant hear - it's still just a local variable. Each time you call `sample::convert`, you make a new `sender`. – Brian61354270 Jul 14 '20 at 14:01
  • What you ask, exactly, can't be done. Normal variable defined in one function ceaces to exist when the function returns. There are several things which do similar things, but we need to know what is your purpose, why you want to do this. – hyde Aug 26 '20 at 04:18

4 Answers4

5

No that's not possible.

Why don't you have sender as a member variable and make sample a class if it is currently a namespace?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

If the actual problem is that you don't know how to define constant member variables, it's just like you define it in the function itself:

class sample
{
    const std::string sender = "sample";
    // Other members...
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

There are two known methods.

First, return the string to use it somewhere (it's probably not what you wanted, but it'll work).

std::string sample::convert()
{
    const std::string sender = "sample"
    return sender;
}

void sample::write()
{
   //I want to use sender variable here like below
   std::string var;
   var = sender();
}

Or, better declare this variable as a class member variable:

class sample {
    std::string sender = "sample"; // if not it's going to be modified, then use 'const'
public:
    ...
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
-1

I got the answer finally.

We need to declare a char * globally. Then using const_cast <char *> we can convert the constant string to char and assign it.

Example: in .h file:

char * abc;

in .cc file:

func()
    {
     const std::string cde = "Hello";
     //now to use this constant string in another function,we use const cast and 
     //assign it to abc like below
     abc = const_cast <char *>(cde.c_str());
    }
  • 1
    This is a repeat of your [other bad answer](https://stackoverflow.com/a/63590232/445976). This doesn't do what you think it does. When the function returns the string variable `cde` will be destroyed. Then the pointer `abc` is left danging. This code is utterly broken. – Blastfurnace Aug 26 '20 at 04:02
  • The person that upvoted this obviously wrong answer is making Stack Overflow a worse place. Congrats, I guess... – Blastfurnace Aug 26 '20 at 04:16