有没有办法在临时声明的对象上调用方法而不必强制将第一个对象分配给变量?
见下文:
class Test
{
private $i = 7;
public function get() {return $this->i;}
}
$temp = new Test();
echo $temp->get(); //ok
echo new Test()->get(); //invalid syntax
echo {new Test()}->get(); //invalid syntax
echo ${new Test()}->get(); //invalid syntax
Run Code Online (Sandbox Code Playgroud) 我一直认为C++中的临时对象会被编译器自动视为const.但最近我经历了以下代码示例:
function_returning_object().some_non_const_method();
对C++编译器有效.它让我想知道 - 是C++ const中的临时对象吗?如果是,那么为什么编译器认为上面的代码是正确的?
可能的重复:
typedef 和 const 指针的容器
为什么代码会发出错误?
int main()
{
//test code
typedef int& Ref_to_int;
const Ref_to_int ref = 10;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
错误:从“int”类型的临时对象初始化“int&”类型的非常量引用无效
从ildjarn读完这个答案之后,我编写了以下示例,它看起来像一个未命名的临时对象与其引用具有相同的生命周期!
源代码:
#include <iostream> //cout
#include <sstream> //ostringstream
int main ()
{
std::ostringstream oss;
oss << 1234;
std::string const& str = oss.str();
char const* ptr = str.c_str();
// Change the stream content
oss << "_more_stuff_";
oss.str(""); //reset
oss << "Beginning";
std::cout << oss.str() <<'\n';
// Fill the call stack
// ... create many local variables, call functions...
// Change again the stream content
oss << "Again";
oss.str(""); //reset
oss << "Next should …Run Code Online (Sandbox Code Playgroud) 抱歉,我知道存在类似的问题,但我仍然不完全清楚。以下安全吗?
void copyStr(const char* s)
{
strcpy(otherVar, s);
}
std::string getStr()
{
return "foo";
}
main()
{
copyStr(getStr().c_str());
}
Run Code Online (Sandbox Code Playgroud)
临时 std::string 将存储 getStr() 的返回值,但它的寿命是否足以让我将其 C 字符串复制到其他地方?或者我必须明确地为其保留一个变量,例如
std::string temp = getStr();
copyStr(temp.c_str());
Run Code Online (Sandbox Code Playgroud) 我使用了mysql 5.7.16社区和centos 7.
我正在关注安装mysql 的视频教程:
启动mysql后,我可以看到状态是活着的(正在运行)了service mysqld status.
但我可以通过以下方式得到任何东西 grep 'temporary password' /var/log/mysqld.log
msyqld.log(忽略普通日志):
[Warning] InnoDB: Cannot open table mysql/plugin from the internal data dictionary of InnoDB though the .frm file for the table exists.
mysqld: Table 'mysql.plugin' doesn't exist
[ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
[Warning] InnoDB: Cannot open table mysql/gtid_executed from the internal …Run Code Online (Sandbox Code Playgroud) 以下代码创建一个临时Vector:
0.to(15).map(f).toArray
^^^^^^^^
Sequence
^^^^^^^^^^^^^^^
temp Vector
^^^^^^^^^^^^^^^^^^^^^^^
Array
Run Code Online (Sandbox Code Playgroud)
以下代码创建一个临时数组:
0.to(15).toArray.map(f)
^^^^^^^^
Sequence
^^^^^^^^^^^^^^^
temp Array
^^^^^^^^^^^^^^^^^^^^^^^
Array
Run Code Online (Sandbox Code Playgroud)
有没有办法在序列上映射f并直接获取数组,而不产生临时?
我有一个函数返回对我的类"记录"的实例的引用.
record& get_record(int key) {
return lookup(key);
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,它返回一个引用而不是值.现在我稍微修改一下.
record& get_record(int key) {
if (valid(key))
return lookup(key);
else {
record x;
x.valid=false;
return x; //Here I really want to return a temporary variable
// and not a reference to a local variable.
}
}
Run Code Online (Sandbox Code Playgroud)
返回对局部变量的引用不是一个好主意是否正确?以及如何以一种临时变量的方式返回x?
是以下代码安全(它在DEBUG中工作):
void takesPointer(const Type* v);//this function does read from v, it doesn't alter v in any way
Type getValue();
...
takesPointer(&getValue());//gives warning while compiling "not an lvalue"
...
Type tmp = getValue();
takesPointer(&tmp);//this is safe, and maybe I should just do it, instead of posting here
Run Code Online (Sandbox Code Playgroud)
所以 - 这样安全吗?我应该忘记它并使用显式tmp的代码吗?
但无论如何 - 如果允许优化器在从此调用返回之前杀死临时值,我仍然感兴趣:
takePointer(&getValue())
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢大家!不幸的是我无法更改函数"takePointer"(它是库的一部分),我只能将它包装在一个函数"takeReference"中,它调用takePointer - 这会消除副本,还是编译器仍然可以创建一个副本("类型"是一个int-3x3-Matrix,所以它不会那么糟糕,但仍然......)?
inline void takesReference(const Type& v){ takesPointer(&v); }
Run Code Online (Sandbox Code Playgroud)
关于破坏的时间:在"takePointer"返回之后,还是在它被调用之后它会被销毁?
请考虑以下代码段:
#include <iostream>
using namespace std;
class Temp {
public:
Temp() { cout << "Temp()" << endl;}
~Temp() { cout << "~Temp()" << endl;}
};
Temp GetTemp() {
cout << "GetTemp" << endl;
return Temp();
}
Temp TakeTemp(Temp temp) {
cout << "TakeTemp" << endl;
return temp;
}
int main()
{
TakeTemp(GetTemp());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我跑TakeTemp(GetTemp());,输出看起来像
GetTemp
Temp()
TakeTemp
~Temp()
~Temp()
Run Code Online (Sandbox Code Playgroud)
注意,~Temp()这里调用两次(但只构造了1个temp obj).这似乎很奇怪,因为1)返回的临时变量GetTemp()应该将其生命周期扩展到完整表达式,并且2)因为我们temp直接TakeTemp返回,返回值optmization将重用相同的对象.
任何人都可以解释为什么这里有多个dstor调用?
(注意,如果我们放置更多层的TakeTemp(),dstor调用的数量会按比例增长.)