临时变量的范围 [英] Scope of temporary variables

查看:90
本文介绍了临时变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在C ++中确定临时变量

的范围规则。我知道做的事情如下:


string s(" abc");

const char * t =(s +" def")。 c_str();

cout<< t;


无效,因为(s +def)创建一个超出范围的临时值,

因此留下一个悬空指针。我想知道的是,当他们的表达式终止时,或者当他们的语句终止时,是否
临时值超出范围。例如,以下是错误的吗?


string s(" abc");

cout<< (s +" def")。c_str();

解决方案

ge *********** @ gmail.com 写道:


我一直试图在C ++中确定临时变量的范围规则

。我知道做的事情如下:


string s(" abc");

const char * t =(s +" def")。 c_str();

cout<< t;


无效,因为(s +def)创建一个超出范围的临时值,

因此留下一个悬空指针。我想知道的是,当他们的表达式终止时,或者当他们的语句终止时,是否
临时值超出范围。



如果我没记错的话,范围是

temporary是子表达式的最大表达式。 (然后有一些关于功能

的电话)。


例如,以下是错误的吗?

string s(" abc");

cout<< (S + QUOT; DEF")。c_str();



自从''cout<< (s +" def")。c_str()''是一个表达式。

最好


Kai-Uwe Bux


Kai-Uwe Bux写道:

ge *********** @ gmail.com 写道:


我一直试图确定范围界定临时变量的规则
C ++中的
。我知道做的事情如下:


string s(" abc");

const char * t =(s +" def")。 c_str();

cout<< t;


无效,因为(s +def)创建一个超出范围的临时值,

因此留下一个悬空指针。我想知道的是,当他们的表达式终止时,或者当他们的语句终止时,是否
临时值超出范围。



如果我没记错的话,范围是

temporary是子表达式的最大表达式。 (然后有一些关于功能

的电话)。


例如,以下是错误的吗?

string s(" abc");

cout<< (S + QUOT; DEF")。c_str();



自从''cout<< (s +" def")。c_str()''是表达式。


最佳


Kai-Uwe Bux



好​​点。我想我应该在函数调用形式中问它:

string s(" abc");

printf("%s \ n",( s +" def")。c_str());


ge *********** @ gmail.com 写道:


Kai-Uwe Bux写道:


> ge ***** ******@gmail.com 写道:


我一直试图确定临时变量的范围规则
C ++中的
。我知道做的事情如下:


string s(" abc");

const char * t =(s +" def")。 c_str();

cout<< t;


无效,因为(s +def)创建一个超出范围的临时值,

因此留下一个悬空指针。我想知道的是,当他们的表达式终止时,或者当他们的语句终止时,是否
临时值超出范围。


如果我没记错的话,范围是
临时是子表达式的最大表达式。 (然后有一些关于
函数调用的东西)。


例如,以下是错误的吗?


string s(" abc");

cout<< (S + QUOT; DEF")。c_str();


自从'cout<< (s +" def")。c_str()''是一个表达式。

最好

Kai-Uwe Bux


好​​点。我想我应该在函数调用形式中问它:

string s(" abc");

printf("%s \ n",( S +" DEF")c_str());



我认为,''printf("%s \ n",(s +" def")。c_str())''是一个表达式也是。它原来是
类型为void。通常,在C ++中,函数调用只是一种特殊形式的表达式。声明再次是一个表达式

语句,该函数只是在评估

表达式时调用(有点奇怪的是,它可以是void类型! )。


换句话说,你也应该没事。

最好


Kai-Uwe Bux


I''ve been trying to pin down the scoping rules for temporary variables
in C++. I know that doing something like:

string s("abc");
const char *t = (s+"def").c_str();
cout << t;

is invalid since (s+"def") creates a temporary which goes out of scope,
thus leaving t a dangling pointer. What I''m wondering is whether
temporaries go out of scope when their expression terminates or when
their statement terminates. For example, is the following wrong?

string s("abc");
cout << (s+"def").c_str();

解决方案

ge***********@gmail.com wrote:

I''ve been trying to pin down the scoping rules for temporary variables
in C++. I know that doing something like:

string s("abc");
const char *t = (s+"def").c_str();
cout << t;

is invalid since (s+"def") creates a temporary which goes out of scope,
thus leaving t a dangling pointer. What I''m wondering is whether
temporaries go out of scope when their expression terminates or when
their statement terminates.

If I recall correctly, the scope is the largest expression of which the
temporary is a subexpression. (And then there was something about function
calls).

For example, is the following wrong?

string s("abc");
cout << (s+"def").c_str();

That would be fine since ''cout << (s+"def").c_str()'' is an expression.
Best

Kai-Uwe Bux


Kai-Uwe Bux wrote:

ge***********@gmail.com wrote:

I''ve been trying to pin down the scoping rules for temporary variables
in C++. I know that doing something like:

string s("abc");
const char *t = (s+"def").c_str();
cout << t;

is invalid since (s+"def") creates a temporary which goes out of scope,
thus leaving t a dangling pointer. What I''m wondering is whether
temporaries go out of scope when their expression terminates or when
their statement terminates.


If I recall correctly, the scope is the largest expression of which the
temporary is a subexpression. (And then there was something about function
calls).

For example, is the following wrong?

string s("abc");
cout << (s+"def").c_str();


That would be fine since ''cout << (s+"def").c_str()'' is an expression.
Best

Kai-Uwe Bux

Good point. I guess I should really ask it in function-call form:
string s("abc");
printf("%s\n", (s+"def").c_str());


ge***********@gmail.com wrote:

Kai-Uwe Bux wrote:

>ge***********@gmail.com wrote:

I''ve been trying to pin down the scoping rules for temporary variables
in C++. I know that doing something like:

string s("abc");
const char *t = (s+"def").c_str();
cout << t;

is invalid since (s+"def") creates a temporary which goes out of scope,
thus leaving t a dangling pointer. What I''m wondering is whether
temporaries go out of scope when their expression terminates or when
their statement terminates.


If I recall correctly, the scope is the largest expression of which the
temporary is a subexpression. (And then there was something about
function calls).

For example, is the following wrong?

string s("abc");
cout << (s+"def").c_str();


That would be fine since ''cout << (s+"def").c_str()'' is an expression.
Best

Kai-Uwe Bux


Good point. I guess I should really ask it in function-call form:
string s("abc");
printf("%s\n", (s+"def").c_str());

I think, ''printf("%s\n", (s+"def").c_str())'' is an expression, too. It
happens to be of type void. Generally, in C++, a function call is just a
special form of expression. The statement is, again, an expression
statement and the function is just called in the course of evaluating the
expression (which, somewhat strangely, can be of type void!).

In other words, you should be fine here, too.
Best

Kai-Uwe Bux


这篇关于临时变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆