Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between temporary variable and constant in C++?

Allow me to post my code first:

void Fun(short &s){}
void FunCon(const short &s){}

int main()
{
  int iTest = 20;//note: iTest is int but parameter of Fun is "short &"
  Fun(iTest);//error, I know there is a temp variable(typecast)
  FunCon(iTest);//ok
  return 0;
}

I know Fun(iTest);will generate a new temp variable(typecast), but I wonder if the temp variable is a constant?

If No: Why can't I pass the temp variable to short &

If Yes: I have another code like this:

class MyObject{
  public :
  void F(){}
};

MyObject MOCreator(){
  return MyObject();
}

int main()
{
   MOCreator().F();//OK
   return 0;
}

If temp variable that returned by MOCreator() is constant, why the temp variable can call non-const member function?

My questions are:

1) What is the difference between temporary variable and constant in C++?

2) There is a sentence in Thinking in C++(page 507). Is the sentence right? and why?:

Temporary objects are automatically const

I was asked for a simple question by someone, and I encounter more questions on my way to solving the question. I do know they may be a very common questions,and I am searching for a long time on net. I also got many different answers. But I'm more confused about it now.

Thanks in advance.

like image 912
Joe Avatar asked Mar 31 '16 05:03

Joe


People also ask

What is temporary variable in C?

In computer programming, a temporary variable is a variable with short lifetime, usually to hold data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short-lived, it is usually declared as a local variable, i.e., a variable with local scope.

Why is temp used in C?

Swap Numbers Using Temporary Variable In the above program, the temp variable is assigned the value of the first variable. Then, the value of the first variable is assigned to the second variable. Finally, the temp (which holds the initial value of first ) is assigned to second . This completes the swapping process.

What is temp in C Plus Plus?

C++ doesn't have anything called Temp. It is likely a variable that the person who wrote the code picked for some reason. Usually, it means the value isn't needed for more than a moment (i.e. it is a temporary value to be calculated, used, and forgotten.)


3 Answers

I wonder if the temp variable is a constant?

No, it's just a temporary variable which will be destroyed at the end of the full expression in which it was created.

Why can't I pass the temp variable to short &

A temporary can't be bound for a non-const reference. But it could be bound for const reference, and the lifetime of the temporary will be extended to match the lifetime of the reference. That's why FunCon(iTest); is fine.

why the temp variable can call non-const member function?

It's fine. The only special thing is the temporary variable will be destroyed at the end of the full expression.

Is the sentence right? and why?
Temporary objects are automatically const

No. Unless you explicitly declare it to be const.

like image 165
songyuanyao Avatar answered Oct 12 '22 18:10

songyuanyao


Anonymous temporaries are rvalues, which can only be bound to an rvalue reference or a const lvalue reference. So your temporary short can be bound to a short&& or a const short&.

They are not const; the main purpose of rvalue references is to alter them by moving expensive-to-copy resources out of them and into a new object.

like image 39
Miles Budnek Avatar answered Oct 12 '22 16:10

Miles Budnek


Given the following:

void f(short& s) {
   s = 42;
}

Now consider this simple use case:

short s = 0;
f(s);
std::cout << s << "\n";

This will print 42 - calling f had the side effect of modifying s.

But now consider the following, if it were legal:

int i = 0;
f(i);
std::cout << i << "\n";

This would print 0 because what you would be modifying in f is the temporary, with the original i untouched.

To prevent this behavior, you can only pass a temporary to a const reference or an rvalue reference.

like image 28
kfsone Avatar answered Oct 12 '22 17:10

kfsone